Play Sound While on Call - Windows Mobile Development and Hacking General

Hello,
Does anyone know how to play a sound (preferably through the earpiece) while on a phone call? I found some code on the internet somewhere that allows me to play sound, but when I make a call, it doesn’t work. Here is the code I have now.
Code:
Public Class Sound
Private m_soundBytes() As Byte
Private m_fileName As String
Public Declare Function WCE_PlaySound Lib "CoreDll.dll" Alias "PlaySound" (ByVal szSound As String, ByVal hMod As IntPtr, ByVal flags As Integer) As Integer
Public Declare Function WCE_PlaySoundBytes Lib "CoreDll.dll" Alias "PlaySound" (ByVal szSound() As Byte, ByVal hMod As IntPtr, ByVal flags As Integer) As Integer
Private Enum Flags
SND_SYNC = &H0 ' play synchronously (default)
SND_ASYNC = &H1 ' play asynchronously
SND_NODEFAULT = &H2 ' silence (!default) if sound not found
SND_MEMORY = &H4 ' pszSound points to a memory file
SND_LOOP = &H8 ' loop the sound until next sndPlaySound
SND_NOSTOP = &H10 ' don't stop any currently playing sound
SND_NOWAIT = &H2000 ' don't wait if the driver is busy
SND_ALIAS = &H10000 ' name is a registry alias
SND_ALIAS_ID = &H110000 ' alias is a predefined ID
SND_FILENAME = &H20000 ' name is file name
SND_RESOURCE = &H40004 ' name is resource name or atom
End Enum
' Construct the Sound object to play sound data from the specified file.
Public Sub New(ByVal fileName As String)
m_fileName = fileName
End Sub
' Construct the Sound object to play sound data from the specified stream.
Public Sub New(ByVal stream As IO.Stream)
' read the data from the stream
m_soundBytes = New Byte(stream.Length) {}
stream.Read(m_soundBytes, 0, Fix(stream.Length))
End Sub 'New
' Play the sound
Public Sub Play()
' If a file name has been registered, call WCE_PlaySound,
' otherwise call WCE_PlaySoundBytes.
If Not (m_fileName Is Nothing) Then
WCE_PlaySound(m_fileName, IntPtr.Zero, Fix(Flags.SND_ASYNC Or Flags.SND_FILENAME))
Else
WCE_PlaySoundBytes(m_soundBytes, IntPtr.Zero, Fix(Flags.SND_ASYNC Or Flags.SND_MEMORY))
End If
End Sub
End Class
I am writing this for my Kaiser on WM6, but support for earlier devices would be nice if possible.
Thanks,
Jay

i'm also trying to create an application that vibrates the phone whenever call is connected but somehow the OS mutes all sounds and vibrations while you are making a call.
The code you have, IMHO, plays the sound through the loudspeaker, not the earpiece.

what you would have to do, because i have done it, is set the registry settings under the control panel (under local machine) and there is an option to set different sounds for differnt events/actions/calls/messages etc

try API PlayEventSound

this idea is already integrated in LC minutes though i didnt have much time to work it out like it should be.
It would be nice to have a complete customization of this idea.

mrpotter said:
try API PlayEventSound
Click to expand...
Click to collapse
Thank you mrpotter. PlayEventSound did what I was looking for

It may play a sound environmental background during a call to simulate being in a traffic jam or in the countryside?

Example
Can anyone post an example of how to use API playeventsound in .net ?

Related

GPRS connectivity in EVB

I have an EVB application that connects to GPRS using a library from Sapphire Solutions.
This has worked reasonably well, but doesn't give me a lot of control of what is going on.
What other methods have people tried? Any hints?
here
I also was lookinf for this solution forever and never found it so i wrote the software myself. Here is the dll that you need to connect to the internet through evb it connects to the default connection no problems.
http://xdaconnect.wastedbrains.com
have fun,
Dan Mayer
Thanks for this. I'll have a play.
The stupid thing is, O2 have told me that I should be using their routines. But nobody can tell me what the routines are, how to find them or give me any information about them whatsoever!
Compare and contrast with Vodafone who actually know a thing or two about implementing mobile systems, but don't have a product to implement...
Code:
Public Declare Function AllocPointer Lib "VBPointers" Alias "VB_AllocPointer" (ByVal nSrcSizeInBytes As Long) As Long
Public Declare Function AllocStringPointer Lib "VBPointers" Alias "VB_AllocStringPointer" (ByVal str As String) As Long
Public Declare Function FreePointer Lib "VBPointers" Alias "VB_FreePointer" (ByVal Pointer As Long) As Long
Public Declare Sub SetLongAt Lib "VBPointers" Alias "VB_SetLongAt" (ByVal Pointer As Long, ByVal OffsetInBytes As Long, ByVal Value As Long)
Public Declare Function PhoneMakeCall Lib "Phone" (ByVal CallInfo As Long) As Long
Public Sub Dial(Number_to_dial As String)
CallInfo = AllocPointer(24) ' sizeof (PHONEMAKECALLINFO)
Call SetLongAt(CallInfo, 0, 24) ' cbSize
Call SetLongAt(CallInfo, 4, 1) ' dwFlags = PMCF_DEFAULT
PhoneNumber = AllocStringPointer(Number_to_dial)
Call SetLongAt(CallInfo, 8, PhoneNumber) ' pszDestAddress
PhoneMakeCall (CallInfo)
FreePointer (PhoneNumber)
FreePointer (CallInfo)
End Sub
The xdaconnect dll posted by Dan is pretty good at getting a connection. The problem is that it has to be registered on each device using eVC. My problem is that I want to make a cd so that other people who dont have eVC can install the device. Normally I would do this with dllRegisterServer or by including the file in the manifest file of dll's to be registered when a program is installed. The problem is that this dll wont register alone, an error is returned back that "dllRegsisterServer is not included in the dll".
Can anyone help?
The dll and eVC project file is available for download at http://xdaconnect.wastedbrains.com
Many thanks
Tony

lineGetID returns a NULL handle

Hi everybody
I tried to achieve a handle for readFile/writeFile operations using lineGetID.
The call to the function passed without errors. My problem is that the handle that I get is 0..., and I should make IO operations some how...
My code:
lRet := lineGetID( FLineHandle, 0, 0, LINECALLSELECT_LINE, PVarString, szClassType );
if lRet = 0 then
begin
FDevHandle := (LPHANDLE(PByte(PVarString) + PVarString^.dwStringOffset ))^;
// some more non relevant code which checks the validity of FDevHandle....
end;
The call to lineGetID is done after successful call to lineGetOpen. I test my application on i-mate jam with WM2003.
Thanks ahead
Kobi
I solved the problem
Hi everybody
For those who encounters the same problem as mine here is the solution that I finally found:
1. The Handle is NULL if we call the lineGetID using LINECALLSELECT_LINE. We should call lineGetID after getting into the LINECALLSTATE_CONNECTED state and use the LINECALLSTATE_CALL parameter. This information was found in the MSDN.
2. The lineGetID (as well as other line functions) doesn't report (I use TAPI 2.0 with WM-2003 on i-mate jam - the cellular line channel) the error LINEERR_STRUCTURETOOSMALL, so immediately after calling the lineGetID I added the following line (I use free pascal compiler):
if (Result = 0) and (PVarString^.dwTotalSize < PVarString^.dwNeededSize) then
Result := integer(LINEERR_STRUCTURETOOSMALL);
After this line comes the characteristic TAPI code which checks if either we should reallocate the previous block, or we can already use the data that call should have given to us.
Just after considering these 2 issues I got a vaild Handle...
Kobi
Hi Kobi,
I also having difficulties creating handle to write data on this TAPI Line, Can you please help me translate your code in VB.NET or C#, I am using VB and C# on developing application in WM2003.
Many thanks in Advance,
Eefendi
More complete code
Hi Eefendi
I neither know VB.NET nor C#, so it will be in Pascal, but I will add comments.
The code itself should be activated after you received the LINE_CALLSTATECONNECTED message/event (depends on the mechanism you selected when you called lineInitialize).
The CallHandle is a variable holds the handle of the call you want to achieve a handle to its stream (you achieve CallHandle, in incoming call for example, using LINE_CALLSTATEOFFERING message/event). The final result we want is stored in Handle. The line itself should be opened using "comm" or "comm/datamodem" class type.
var
VarSize,
Res : integer;
PVarString : LPVARSTRING;
szClassType : PWideChar = 'comm/datamodem';
// these variables definitions is equivalent to the following C code:
// int VarSize, Res;
// LPVARSTRING PVarString;
// TCHAR *szClassType = "comm/datamodem";
begin
VarSize := sizeof(VARSTRING);
Handle := 0;
repeat
GetMem( PVarString, VarSize ); // alloc VarSize bytes
PVarString^.TotalSize := VarSize;
Res := lineGetID( 0, 0, CallHandle, LINECALLSELECT_CALL, PVarString, szClassType );
if (Res = 0) and (PVarString^.TotalSize < PVarString^.NeededSize) then
Res := LINEERR_STRUCTURETOOSMALL;
if Res = LINEERR_STRUCTURETOOSMALL then
VarSize := PVarString^.NeededSize
else
if Res = 0 then // no error
Handle := LPHANDLE(PByte(PVarString) + PVarString^.dwStringOffset)^;
FreeMem( PVarString, PVarString^.TotalSize ); // free memory
until Res <> LINEERR_STRUCTURETOOSMALL; // finish the repeat until loop if this condition is TRUE
if (Handle <> 0) and (Handle <> INVALID_HANDLE_VALUE) then
//;use it as you wish....
end;
Some help to you for conversion:
1. PVarString^.TotalSize using C means PVarString->TotalSize
2. PByte using C means char*
3. type cast for example to PByte is done by PByte(...) while using C it is done by (PByte)...
4. Refering to a value which is referenced by the pointer P and storing it in variable X is done by "X := P^;". using C it is done by "X = *P;".
5. The operator <> means not equal. in C it is !=.
I hope it will be easy to you to translate it to C or any other language you want.
Good luck with your project.
Kobi
TAPI does seem to pop up a lot.
Have you looked at my code I posted a while back here ?
http://forum.xda-developers.com/viewtopic.php?t=18978
Hi Vangelderp and Kobi,
thanks for spending your time answering my questions and also thanks for the code provided. I use VB.Net 2003 on programming my pocket PC 2003, you guys provide me the code using pointer data type which is not supported in VB.net, so I am having trouble converting it. ( When I was learning C++, pointer is always make me confuse :roll: ). I wish you guys will be kind enough helping me convert it to VB.Net, just for small part to get Handle. :wink:
Mant Thanks in advance,
Eefendi

How can i access VIBRATE from VB-NET

Hello,
i´m writing an application with VB-NET, and i want to access the VIBRATE function. But ...
This API is not supported by Windows Mobile 6 Professional
i´m not able to get this API.
On the MS Homepage i found many function with the funny remark.
How can i make my phone vibrate with vb-net (VSO08P)
and how can i find a offline documentation for the WM6.1 Prof. device ??
Thx.
Daniel
Isn’t possible to access all functions from .NET (VB.NET, C#, etc). Some time is necessary to use unmanage code.
To import a dll you must use function <DllImport(".dll"> (must specify the dll name)
P.S. Excuse my English
Search for SndSetSound on aygshell.dll .
C#
[DllImport("aygshell.dll", SetLastError = true)]
private static extern uint SndSetSound(SoundEvent
seSoundEvent, ref SNDFILEINFO pSoundFileInfo, bool fSuppressUI);
A search at google will give you the rest.
this might work:
Declare Function Vibrate Lib "aygshell.dll" (ByVal cvn As Integer, ByVal rgvn As IntPtr, ByVal fRepeat As Boolean, ByVal dwTimeout As UInteger) As Integer
Declare Function VibrateStop Lib "aygshell.dll" () As Integer
Vibrate(0, IntPtr.Zero, vbTrue, INFINITE)
Const INFINITE As UInteger = 4294967295
Many thanks for the code, but on my device
i don´t fond the "aygshell.dll".
How can i find this ddl for a HTC Diamond for Win Mobile 6.1 Prof. ??
I only found one for an old HTC2000 (?).
And, if i share a programm for the HTC , it is legal to copy the DLL ??
Daniel
OK - I found a DLL and want to transfer the dll to WINDOWS ... but the file already exist $§%§..
The Scrips don´t work - no error message - no vibrate.
Daniel
danielham said:
Many thanks for the code, but on my device
i don´t fond the "aygshell.dll".
How can i find this ddl for a HTC Diamond for Win Mobile 6.1 Prof. ??
I only found one for an old HTC2000 (?).
And, if i share a programm for the HTC , it is legal to copy the DLL ??
Daniel
OK - I found a DLL and want to transfer the dll to WINDOWS ... but the file already exist $§%§..
The Scrips don´t work - no error message - no vibrate.
Daniel
Click to expand...
Click to collapse
Import aygshell.lib
In MSDN for this functions requirements are:
Pocket PC Platforms: None
OS Versions: Windows CE 3.0 and later
Header: Declared in vibrate.h
Library: Use aygshell.lib
----
Declare Function VibrateStop Lib "aygshell.dll" () As Integer
'Const INFINITE As Integer = 4294967295
Const INFINITE As UInteger = &HFFFFFFFF&
Declare Function Vibrate Lib "aygshell.dll" (ByVal cvn As Integer, ByVal rgvn As IntPtr, ByVal fRepeat As Boolean, ByVal dwTimeout As UInteger) As Integer
----
i tried both version for INFINITE.
First, i work with VB.NET and i don´t find "IMPORT DLL...."
Second, i get a warning message for UInteger dwTimeout, as Integer i get no warning ?
The first statement above are in the Class FORM1
----
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Vibrate(0, IntPtr.Zero, vbTrue, INFINITE)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
VibrateStop()
End Sub
----
And this statement don´t work ...
I want to vibrate if i press a key ...
a "hello world" for vibrate for beginners
Daniel

accelerometer(lg gm750) method signatures

attached the.dll of the lg gm750 in case they serve of something
USER iamspv
First take a look at the Unified Sensor Api at codeplex.
My solution is based on the Samsung sensor class, modified to meet the peculiarities of the LG GM750.
First of all, the import of the DeviceIoControl in NativeMethods.cs must be modified such way dwIoControlCode is type uint, and the buffers are byte:
Code:
[DllImport("coredll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode, [In] byte[] inBuffer, int nInBufferSize, [Out] byte[] outBuffer, int nOutBufferSize, ref int pBytesReturned, IntPtr lpOverlapped);
After a long time dissassembling the accelsensor.dll and and a lot of trial and error testing I came to this conclusion that works:
The constant that starts/stops data output becomes:
Code:
const uint ACCOnRot = 0x1014EE8;
const uint ACCOffRot = 0x1014EE8;
and for reading values:
Code:
const uint ACCReadValues = 0x1014F10;
The difference between start and stop comes from the input given in DeviceIoControl method as follows:
Code:
Code:
public static LGAccSensor Create()
{
DeviceIoControl(ACCOnRot, new byte[1] {1}, new byte[4] );
return new LGAccSensor();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
DeviceIoControl(ACCOffRot, new byte[1] {0}, new byte[1] );
}
The DeviceIoControl inside the accelerometer class becomes
Code:
Code:
public static void DeviceIoControl(uint controlCode, byte[] inBuffer, byte[] outBuffer)
{
IntPtr file = NativeMethods.CreateFile("ACC1:", 0, 0, IntPtr.Zero, ECreationDisposition.OpenExisting, 0, IntPtr.Zero);
if (file == (IntPtr)(-1))
throw new InvalidOperationException("Unable to Create File");
try
{
int bytesReturned = 0;
int inSize = sizeof(byte) * inBuffer.Length;
int outSize = sizeof(byte) * outBuffer.Length;
if (!NativeMethods.DeviceIoControl(file, controlCode, inBuffer, inSize, outBuffer, outSize, ref bytesReturned, IntPtr.Zero))
throw new InvalidOperationException("Unable to perform operation.");
}
finally
{
NativeMethods.CloseHandle(file);
}
}
Note that the accelerometer device is called ACC1:.
Further the method that returns the three component vector becomes
Code:
Code:
public override GVector GetGVector()
{
byte[] outBuffer = new byte[4];
DeviceIoControl(ACCReadValues, new byte[1] {1}, outBuffer);
GVector ret = new GVector();
int accx = outBuffer[2];
if (accx <=31)
ret.X = accx/2.17;
else
ret.X = (accx-64)*0.47;
ret.X = -ret.X;
int accy = outBuffer[1] ;
if (accy <=31)
ret.Y = accy/2.17;
else
ret.Y = (accy - 64) * 0.47;
int accz = outBuffer[0] ;
if (accz <=31)
ret.Z = accz/2.17;
else
ret.Z = (accz - 64) * 0.47;
double somefactor = 1; return ret.Scale(somefactor);
}
Note that the when called with AccReadValues parameter, the DeviceIoControl returns three bytes each of them containing a signed 6 bit 2's complement value.Basicly there are values on 6 bit ranging from -32 to +31. In the docs below there are tables that put in correspondence these values with acceleration in range -1.5g +1.5g which are the limits of the device. The code I wrote returns acceleration in meters/second2 where 1g=9.8 m/s2. That information I extracted from the technical specifications of the MMA7660FC accelerometer, as found at(try a google search). Also, the type of the accelerometer circuit is found in the service manual (I only found the service manual for LG GM730 which is pretty close).The same thing I also posted at codeplex.
Reply With Quote
It already works! There is nothing more to do about it than use the code in your applications (I speak as a programmer and for programmers). I only showed everyone interested in developing for LG GM750 the steps to modify the SamsungGSensor class from "Unified Sensor Api" at codeplex to work with a LG. Just get the rest of the code from there and put it toghether.
Now there are two directions:
Direction #1
If you want to develop applications that aim to be portable between HTC, Samsung, LG, Toshiba, etc, and you want to use Unified Sensor Api then..
the person responsible for the unified api project at codeplex should make the update to the library as soon as possible such way the library should keep compatibility with the adressed devices. I'm just contributing with some results. That's because the initial code was not very general (ie. the initial parameter types to deviceiocontrol invoked from coredll didn't work with this solution) I had to make some modifications to the code that work with the LG but affect other devices' sensor classes. Now, if the update doesn't come up soon, no problem, anyone just gets the code from codeplex, adds a new class for LG, copies code from Samsung, renames Samsung with LG, adds my modifications, corrects syntax errors, compiles, et voila . . the library is updated to support LG GM750.
Direction #2
If you develop only for LG, just write a LG class as presented above and don't care about other devices.
If you don't know how to use your new class, the project at codeplex has some examples and you'll easily see inside the code how to instantiate sensors, how to read data at certain moments using a timer, and how to capture screen rotation events.
And for your long awaited joy to see it working, I attached the sensor test program along with a version of sensors.dll compiled by me, that works for GM750.
Now get your fingers toghether on the keyboard and start developing for LG
now should work in an emulator as that one of htc in omnia splitting of what we arrange
great work!
thanks much!
htcemu v0.91 /sensors.dll Problems
Hello all,
when I last November asked for a htcemu for LG750, I have not expected that there will be a emu dll so soon.
Thanks to iamspv and others, we now can use g-sensor apps as normal.
Nearly - some apps does not work or crash.
So my favorite alarm clock, klaxon crashes when alarm is activated. Waterlevel also does not work. It looks that both apps use the managed code dll sensors.dll. Even if I exchange the original dll with the one posted here from beginning of february, it does not work. Does anybody has a solution for this ?
For using landscape mode in app I now got best results with changescreen which works almost perfect. It is also possible to exclude some programs like some LG apps which do not support landscape mode.
how did you make other apps work? on my layla only test works and nothing else
creative232 said:
attached the.dll of the lg gm750 in case they serve of something
Click to expand...
Click to collapse
Did you create a running VS 2008 solution for this?. I'm interested.
Will this work on other LG's?

File transfer over TCP sockets

I've been trying to get a simple file transfer between desktop app and phone app. The transfer works up to a point, when it simply ...stops dead.
The server(aka desktop client) enters the listening state, and the phone goes idle.
Anyone has any samples on transfers of large file (bigger than 1 MB)?
mcosmin222 said:
I've been trying to get a simple file transfer between desktop app and phone app. The transfer works up to a point, when it simply ...stops dead.
The server(aka desktop client) enters the listening state, and the phone goes idle.
Anyone has any samples on transfers of large file (bigger than 1 MB)?
Click to expand...
Click to collapse
Have you looked through GoodDayToDie's source code for the File Server? I wonder if he has anything in there that could make that work.
snickler said:
Have you looked through GoodDayToDie's source code for the File Server? I wonder if he has anything in there that could make that work.
Click to expand...
Click to collapse
lalz.
Completely forgot about that one xD
Meh he has it written in C++
Apparently, he didn't do anything that I didn't.
mcosmin222 said:
lalz.
Completely forgot about that one xD
Meh he has it written in C++
Click to expand...
Click to collapse
You can still utilize the transfer portion . I was thinking of seeing what I could do with sockets on the phone. I know it could come in handy somehow
snickler said:
You can still utilize the transfer portion . I was thinking of seeing what I could do with sockets on the phone. I know it could come in handy somehow
Click to expand...
Click to collapse
It's a pain in the ***.
It stops transfer at random points.
mcosmin222 said:
It's a pain in the ***.
It stops transfer at random points.
Click to expand...
Click to collapse
That doesn't surprise me at all for some reason.
Did you double-check your socket multithreading code?
I recently had problems with sockets and it turned out that I had the muti-threading thing wrong.
I think you shouldn't use only one connection and fail if it drops ...
ScRePt said:
Did you double-check your socket multithreading code?
I recently had problems with sockets and it turned out that I had the muti-threading thing wrong.
I think you shouldn't use only one connection and fail if it drops ...
Click to expand...
Click to collapse
What do you mean by socket multthreading code? You mean the use of async methods? or having the thread work on background, using the socket?
Take a look to the Tim Laverty's networking samples.
sensboston said:
Take a look to the Tim Laverty's networking samples.
Click to expand...
Click to collapse
That's what im doing (more or less)
@mcosmin222: The most common reason I saw for why that happened was the thread doing the transfer would crash. There's a lot of things that could cause such a crash, but because it's not the main thread or a UI thread, you don't see it. It just stops. In fact, even the debugger usually doesn't catch it (annoying as hell...)
There are a few common things that non-UI threads aren't allowed to do which you might be trying. For example, attempting to show a MessageBox on a non-UI thread will crash the thread (you can do it by adding a lambda or function to the dispatcher for the UI). In any case, feel free to use or adapt my code, or share yours here and if there's an obvious issue I'll point it out. Incidentally, you can set a larger buffer on the socket if you want the operation to complete without looping.
By the way, the only portion of my webserver that's written in C++ is the file I/O code, which I chose to do in C++ rather than .NET because the phone's stunted .NET framework makes it more difficult than I like to access arbitrary file paths. That code is all fairly clean wrappers around the Win32 calls; I suppose I could comment it more but it's very straightforward to read even if you aren't familiar with managed C++. The actual network code is entirely written in C# 4.5. You could actually simplify it a bit for a direct transfer app, too; I wrote it with a lot of multithreading in case I wanted to re-use the code somewhere that might be expected to have more than one client connecting at a time.
GoodDayToDie said:
@mcosmin222: The most common reason I saw for why that happened was the thread doing the transfer would crash. There's a lot of things that could cause such a crash, but because it's not the main thread or a UI thread, you don't see it. It just stops. In fact, even the debugger usually doesn't catch it (annoying as hell...)
There are a few common things that non-UI threads aren't allowed to do which you might be trying. For example, attempting to show a MessageBox on a non-UI thread will crash the thread (you can do it by adding a lambda or function to the dispatcher for the UI). In any case, feel free to use or adapt my code, or share yours here and if there's an obvious issue I'll point it out. Incidentally, you can set a larger buffer on the socket if you want the operation to complete without looping.
By the way, the only portion of my webserver that's written in C++ is the file I/O code, which I chose to do in C++ rather than .NET because the phone's stunted .NET framework makes it more difficult than I like to access arbitrary file paths. That code is all fairly clean wrappers around the Win32 calls; I suppose I could comment it more but it's very straightforward to read even if you aren't familiar with managed C++. The actual network code is entirely written in C# 4.5. You could actually simplify it a bit for a direct transfer app, too; I wrote it with a lot of multithreading in case I wanted to re-use the code somewhere that might be expected to have more than one client connecting at a time.
Click to expand...
Click to collapse
I am aware that some calls from background threads are not allowed, especially those that have to do with the UI thread.
This is the code for the server. It would seem this one is the problem, somewhere...I just can't see where...
I tried limiting the number of packages sent (that's what the timer is all about).
Code:
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
public class AsynchronousSocketListener
{
// Thread signal.
public static ManualResetEvent allDone = new ManualResetEvent(false);
public static string[] TransferStages = new string[] { "sendmetadataz-length", "sendmetadataz", "file-length", "file" };
public static int Index = -1;
public static List<string> FilePaths = new List<string>();
public static long CurrentStreamPosition = 0;
public static FileStream ifs;
static int pocketspersecond = 0;
static bool LimitExceded = false;
DispatcherTimer timer = new DispatcherTimer();
public static int CurrentArraySize = 0;
public static int FileIndex = 0;
public AsynchronousSocketListener()
{
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
}
void timer_Tick(object sender, EventArgs e)
{
LimitExceded = false;
}
public static void StartListening()
{
// Data buffer for incoming data.
byte[] bytes = new Byte[StateObject.BufferSize];
// Establish the local endpoint for the socket.
// Note: remember to keep the portnumber updated if you change
// it on here, or on the client
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 13001);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
while (true)
{
// Set the event to nonsignaled state.
allDone.Reset();
// Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);
// Wait until a connection is made before continuing.
allDone.WaitOne();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
public static void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
allDone.Set();
// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
// Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
public static void ReadCallback(IAsyncResult ar)
{
String content = String.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
// Read data from the client socket.
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.UTF8.GetString(
state.buffer, 0, bytesRead));
// Check for end-of-file tag. If it is not there, read
// more data.
content = state.sb.ToString();
if (content.IndexOf("<EOF>") > -1)
{
// All the data has been read from the
// client. Display it on the console.
Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
content.Length, content);
// Respond to the client
Send(handler, content);
}
else
{
// Not all data received. Get more.
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
}
}
public static void Send(Socket handler, String data)
{
//handler.SendBufferSize = File.ReadAllBytes(@"D:\MUZICA\Activ - Visez.mp3").Length;
// handler.BeginSendFile(@"D:\MUZICA\Activ - Visez.mp3", new AsyncCallback(SendCallback), handler);
#region cotobatura
data = data.Replace("<EOF>", "");
if (data.Contains("sendmetadataz") && data.Contains("length")==false)
{
data = MainWindow.DataContextModel.Files.ElementAt(FileIndex).ToString()+"<EOF>";
byte[] byteData = Encoding.UTF8.GetBytes(data);
// Begin sending the data to the remote device.
handler.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), handler);
}
else if (data.Contains("sendmetadataz-length"))
{
Index++;
if (Index >= MainWindow.DataContextModel.Files.Count)
{
//FileIndex++;
data = "TransfersComplete<EOF>";
}
data = Encoding.UTF8.GetByteCount((MainWindow.DataContextModel.Files.ElementAt(FileIndex).ToString() + "<EOF>").ToString()).ToString();
byte[] MetaDataLength = Encoding.UTF8.GetBytes(data);
handler.SendBufferSize = MetaDataLength.Length;
handler.BeginSend(MetaDataLength, 0, MetaDataLength.Length, 0, new AsyncCallback(SendCallback), handler);
}
else if (data.Contains("file-length"))
{
ifs = File.Open(MainWindow.DataContextModel.Files.ElementAt(FileIndex).Location, FileMode.Open);
byte[] gugu = Encoding.UTF8.GetBytes(ifs.Length.ToString());
handler.SendBufferSize = gugu.Length;
handler.BeginSend(gugu, 0, gugu.Length, 0, new AsyncCallback(SendCallback), handler);
}
else if (data.Contains("file") && data.Contains("length") == false)
{
//byte[] filedata = File.ReadAllBytes(MainWindow.DataContextModel.Files.ElementAt(FileIndex).Location);
//handler.BeginSend(filedata, 0, filedata.Length, 0,
//new AsyncCallback(SendCallback), handler);
byte[] filedata = new byte[150];
for (int i = 0; i < 150; i++)
{
if (CurrentStreamPosition < ifs.Length)
{
filedata[i] = (byte)ifs.ReadByte();
CurrentStreamPosition++;
CurrentArraySize++;
}
else
{
Array.Resize(ref filedata, CurrentArraySize);
break;
}
CurrentArraySize = 0;
}
// if (pocketspersecond == 25) LimitExceded = true;
//Thread.Sleep(1000);
handler.BeginSend(filedata, 0, filedata.Length, 0, new AsyncCallback(SendCallback), handler);
}
//handler.BeginSendFile(MainWindow.DataContextModel.Files.ElementAt(FileIndex).Location, filedata, null, TransmitFileOptions.ReuseSocket, new AsyncCallback(SendCallback), handler );
// What we want to send back in this application is a game move based on what
// has been received. So we call Play on the GameLogic to give us a move to send back
// data = GameLogic.Play(data);
// Convert the string data to byte data using ASCII encoding.
//byte[] byteData = Encoding.UTF8.GetBytes(data);
// Begin sending the data to the remote device.
//handler.BeginSend(byteData, 0, byteData.Length, 0,
// new AsyncCallback(SendCallback), handler);
#endregion
}
public static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket handler = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
This is basically modified from the tick tak toe over sockets sample from MSDN.
The only possible call that would affect the UI is the call to the Console, but the code works fine for a while, after it just crashes.
I tried running the whole thing synchronously on the UI thread, the result appears to be the same.
In the Send method, the first 3 stages work (file-legth, metadata, metadata-length) and a few steps in the file stage (which actually sends the file).
AT some point, I assumed the thread was guilty somehow, but I just can't prove it. Running the thing directly on UI thread does not seem to change anything.
If the async method finishes and the socket gets disposed, the thread would "die".
PS: the entire thing is hosted by a WPF application.
Hmm... OK, there are several bugs here. I'm not sure which, if any, are responsible for the problem. I'd be tempted to overuse try-catch-log (for example, on the Send() function) and debug-print statements, but here are some things I can see that could cause a crash or other unexpected failure:
There is no guarantee that "ifs" is instantiated before use. If you for some reason skip the file-length step, the file step will crash with a null pointer exception.
The entire send function is hugely thread-unsafe. For example, if a second request arrives before you're done servicing the first one (which is entirely possible due to where the event gets signaled) then the values of "ifs" and "CurrentStreamPosition" and so on will be unpredictable at any given time. Since CurrentStreamPosition seems to be monotonically increasing, that's probably not going to cause an out-of-bounds exception, but it could cause you to enter a state where the test "if (CurrentStreamPosition < ifs.Length)" always fails.
The line "data = "TransfersComplete<EOF>";" never does anything; the next line (immediately following it) overwrites that variable. If you cared about that string, too bad.
FileIndex never changes; I hope you're only ever sending one file here...
You don't actually check that the number of bytes sent is the number you meant to send (admittedly, it *should* be, but there are cases where it won't be).
The last 150-byte chunk of every file transfer is truncated to the first byte. This is because "CurrentArraySize" is reset to 0 on every iteration of the byte-read loop (why use a byte-read loop?) so whenever "CurrentStreamPosition < ifs.Length" tests false, the "filedata" array will be resized to one byte (or zero if the file is an exact multiple of 150 bytes, which presumeably be correct).
There are probably more, but that's what jumped out at me (well, and some technically correct stylistic issues, like the "... == false" test). Given that your protocol seems to rely on end-of-message flags, I'm guessing that your problem is that since the last part of the file is almost always truncated, that marker is never getting sent. This probably leads to the client concluding that the server will be sending it more data, which it does by sending another "file" request. The server attempts to respond and immedately hits the CurrentStreamPosition < ifs.Length check, fails, goes to the else case, and tries to send a 1-byte packet containing a NULL byte.
Incidentally, does your file transfer protocol really require that the client request each 150-byte chunk one at a time, using a new TCP connection each time? That's... awfully inefficient.
GoodDayToDie said:
Hmm... OK, there are several bugs here. I'm not sure which, if any, are responsible for the problem. I'd be tempted to overuse try-catch-log (for example, on the Send() function) and debug-print statements, but here are some things I can see that could cause a crash or other unexpected failure:
There is no guarantee that "ifs" is instantiated before use. If you for some reason skip the file-length step, the file step will crash with a null pointer exception.
The entire send function is hugely thread-unsafe. For example, if a second request arrives before you're done servicing the first one (which is entirely possible due to where the event gets signaled) then the values of "ifs" and "CurrentStreamPosition" and so on will be unpredictable at any given time. Since CurrentStreamPosition seems to be monotonically increasing, that's probably not going to cause an out-of-bounds exception, but it could cause you to enter a state where the test "if (CurrentStreamPosition < ifs.Length)" always fails.
The line "data = "TransfersComplete<EOF>";" never does anything; the next line (immediately following it) overwrites that variable. If you cared about that string, too bad.
FileIndex never changes; I hope you're only ever sending one file here...
You don't actually check that the number of bytes sent is the number you meant to send (admittedly, it *should* be, but there are cases where it won't be).
The last 150-byte chunk of every file transfer is truncated to the first byte. This is because "CurrentArraySize" is reset to 0 on every iteration of the byte-read loop (why use a byte-read loop?) so whenever "CurrentStreamPosition < ifs.Length" tests false, the "filedata" array will be resized to one byte (or zero if the file is an exact multiple of 150 bytes, which presumeably be correct).
There are probably more, but that's what jumped out at me (well, and some technically correct stylistic issues, like the "... == false" test). Given that your protocol seems to rely on end-of-message flags, I'm guessing that your problem is that since the last part of the file is almost always truncated, that marker is never getting sent. This probably leads to the client concluding that the server will be sending it more data, which it does by sending another "file" request. The server attempts to respond and immedately hits the CurrentStreamPosition < ifs.Length check, fails, goes to the else case, and tries to send a 1-byte packet containing a NULL byte.
Incidentally, does your file transfer protocol really require that the client request each 150-byte chunk one at a time, using a new TCP connection each time? That's... awfully inefficient.
Click to expand...
Click to collapse
I know it is inefficient, but I'm rather new to sockets. I just want it to get working in a "beta stage" then ill optimize it (hance the FileIndex never increasing, the blatant lack of try-catch blocks).
On the client side, once the bytes in the buffer are processed, the server gets another send that to send the following 150 bytes (i use 150 just for the lulz).
So basically, the workfow is as follows:
ask metadata length >server gives the length >client adjusts buffer>ask metadata
ask metdata >server gives metdata>client processes the data>asks file length
ask file length>server gives file length>client adjusts a huge array of bytes in which the file will reside (i know this is horribly inefficient, but at some point i will write directly to a file stream)>asks for the first 150 bytes in the file.
server gets the request, sends 150 bytes to client>client copies the 150 bytes in the array created earlier, the asks for the next 150.
I am using 150 just to make sure the data never splits in more than one buffer.
When the file transfer occurs, a different message is used to signal the end of transfer. Client side counts the bytes it gets, and when it is equal to the file length, it no longer asks 150 bytes.
The whole thing seems to be safe from crashing until it gets to the part where it sends the file. I am aware that in the code i gave you there's some file streams not getting closed, but i've fixed that and the problem still occurs.
Since The debugger won't help me at all, I decided to use a WCF service instead.
mcosmin222 said:
What do you mean by socket multthreading code? You mean the use of async methods? or having the thread work on background, using the socket?
Click to expand...
Click to collapse
I mean worker threads not async.You will always have to have a thread in the background to "accept". once you accept you "read" in a new thread and the parent thread "accepts" again. Accept will freeze the thread.
On the other side, you simply "connect" and "write" in the same thread.
Read and Write is done in a loop via pre-defined buffers syncronously.
But if you want the server to give a response, the above flow is the other way around, and it is then when things get complicated. (server needs to "connect" and client needs to "accept" over a different set of ports and different threads)
Probably if you want to have reliable connection you will need the server to come back with a response "give me more" or sth.
So, trying to assist, it was my guess that drops or stalls could be because the above flow is not implemented properly.
Edit Oh ho, missed a whole new page so I am sorry if the reply is irrelevant now.
I would suggest you use the sync methods of sockets and not callbacks because is super easier to debug. ThreadPool.QueueSth (ctr + space I dont remember how it's called is your friend to handle threads yourself.
And try to separate pure socket handling from domain handling (lengths, metadata, etc). Send some bytes back and forth, clean-up and then move to domain specific things!
Moving the line that resets CurrentArraySize to outside of the for loop may well sove your problem. I'd try that first.
Optimization involves, among other things, removing try blocks. Unoptimized code, when you're trying to just make thigns work, ought to be full of them.
Don't forget that exceptions will not bubble up the call stack across threads. In addition to threads you create yourself, any async callback will happen on a different thread than the one that called the async function. If an uncaught exception occurs, the thread will die. If enough threads die, the program may crash (or at least hang) due to threadpool exhaustion.
GoodDayToDie said:
Moving the line that resets CurrentArraySize to outside of the for loop may well sove your problem. I'd try that first.
Optimization involves, among other things, removing try blocks. Unoptimized code, when you're trying to just make thigns work, ought to be full of them.
Don't forget that exceptions will not bubble up the call stack across threads. In addition to threads you create yourself, any async callback will happen on a different thread than the one that called the async function. If an uncaught exception occurs, the thread will die. If enough threads die, the program may crash (or at least hang) due to threadpool exhaustion.
Click to expand...
Click to collapse
I avoid try-catch in unoptimized code to see where actual exceptions occur (sometime the debugger doesn't break on exception).
Nop still not working.
The thread still appears to be crashing, even wrapped with try catch.
Do you at least know *which* function it's crashing in? Try putting a breakpoint on each function header and then, when one of them is hit, step through the execution until the thread dies. Work backward from there to find the problem.
GoodDayToDie said:
Do you at least know *which* function it's crashing in? Try putting a breakpoint on each function header and then, when one of them is hit, step through the execution until the thread dies. Work backward from there to find the problem.
Click to expand...
Click to collapse
The send function (the one with the case switch) appears to be crashing.
It executes the function then enters the listening stage (does not execute the callback).

Categories

Resources