/// <summary> /// ClientConnected is a callback that gets called when the server accepts a client connection from the async BeginAccept method. /// </summary> /// <param name="ar"></param> /// <remarks></remarks> public void ClientAccepted(IAsyncResult ar) { // get the async state object from the async BeginAccept method, which contains the server's listening socket Socket mServerSocket = (Socket)ar.AsyncState; // call EndAccept which will connect the client and give us the the client socket Socket mClientSocket = null; try { mClientSocket = mServerSocket.EndAccept(ar); } catch (ObjectDisposedException ex) { // if we get an ObjectDisposedException it that means the server socket terminated while this async method was still active return; } // instruct the client to begin receiving data SocketGlobals.AsyncReceiveState mState = new SocketGlobals.AsyncReceiveState(); mState.Socket = mClientSocket; if (ClientConnected != null) { ClientConnected(mState.Socket); } mState.Socket.BeginReceive(mState.Buffer, 0, SocketGlobals.gBufferSize, SocketFlags.None, new AsyncCallback(ClientMessageReceived), mState); // begin accepting another client connection mServerSocket.BeginAccept(new AsyncCallback(ClientAccepted), mServerSocket); }
/// <summary> /// ClientConnected is a callback that gets called when the server accepts a client connection from the async BeginAccept method. /// </summary> /// <param name="ar"></param> /// <remarks></remarks> public void ClientAccepted(IAsyncResult ar) { // get the async state object from the async BeginAccept method, which contains the server's listening socket Socket mServerSocket = (Socket) ar.AsyncState; // call EndAccept which will connect the client and give us the the client socket Socket mClientSocket = null; try { mClientSocket = mServerSocket.EndAccept(ar); } catch (ObjectDisposedException ex) { // if we get an ObjectDisposedException it that means the server socket terminated while this async method was still active return; } // instruct the client to begin receiving data SocketGlobals.AsyncReceiveState mState = new SocketGlobals.AsyncReceiveState(); mState.Socket = mClientSocket; if (ClientConnected != null) { ClientConnected(mState.Socket); } mState.Socket.BeginReceive(mState.Buffer, 0, SocketGlobals.gBufferSize, SocketFlags.None, new AsyncCallback(ClientMessageReceived), mState); // begin accepting another client connection mServerSocket.BeginAccept(new AsyncCallback(ClientAccepted), mServerSocket); }
public void ServerMessageReceived(IAsyncResult ar) { // get the async state object from the async BeginReceive method SocketGlobals.AsyncReceiveState mState = (SocketGlobals.AsyncReceiveState)ar.AsyncState; // call EndReceive which will give us the number of bytes received int numBytesReceived = 0; numBytesReceived = mState.Socket.EndReceive(ar); // determine if this is the first data received if (mState.ReceiveSize == 0) { // this is the first data recieved, so parse the receive size which is encoded in the first four bytes of the buffer mState.ReceiveSize = BitConverter.ToInt32(mState.Buffer, 0); // write the received bytes thus far to the packet data stream mState.PacketBufferStream.Write(mState.Buffer, 4, numBytesReceived - 4); } else { // write the received bytes thus far to the packet data stream mState.PacketBufferStream.Write(mState.Buffer, 0, numBytesReceived); } // increment the total bytes received so far on the state object mState.TotalBytesReceived += numBytesReceived; // check for the end of the packet // bytesReceived = Carcassonne.Library.PacketBufferSize Then if (mState.TotalBytesReceived < mState.ReceiveSize) { // ## STILL MORE DATA FOR THIS PACKET, CONTINUE RECEIVING ## // the TotalBytesReceived is less than the ReceiveSize so we need to continue receiving more data for this packet mState.Socket.BeginReceive(mState.Buffer, 0, SocketGlobals.gBufferSize, SocketFlags.None, new AsyncCallback(ServerMessageReceived), mState); } else { // ## FINAL DATA RECEIVED, PARSE AND PROCESS THE PACKET ## // the TotalBytesReceived is equal to the ReceiveSize, so we are done receiving this Packet...parse it! System.Runtime.Serialization.Formatters.Binary.BinaryFormatter mSerializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); // rewind the PacketBufferStream so we can de-serialize it mState.PacketBufferStream.Position = 0; // de-serialize the PacketBufferStream which will give us an actual Packet object mState.Packet = (string)mSerializer.Deserialize(mState.PacketBufferStream); // parse the complete message that was received from the server ParseReceivedServerMessage(mState.Packet, mState.Socket); // call BeginReceive again, so we can start receiving another packet from this client socket SocketGlobals.AsyncReceiveState mNextState = new SocketGlobals.AsyncReceiveState(); mNextState.Socket = mState.Socket; mNextState.Socket.BeginReceive(mNextState.Buffer, 0, SocketGlobals.gBufferSize, SocketFlags.None, new AsyncCallback(ServerMessageReceived), mNextState); } }
/// <summary> /// Fires right when a client is connected to the server. /// </summary> /// <param name="ar"></param> /// <remarks></remarks> public void ConnectToServerCompleted(IAsyncResult ar) { // get the async state object which was returned by the async beginconnect method SocketGlobals.AsyncSendState mState = (SocketGlobals.AsyncSendState) ar.AsyncState; // end the async connection request so we can check if we are connected to the server try { // call the EndConnect method which will succeed or throw an error depending on the result of the connection mState.Socket.EndConnect(ar); // at this point, the EndConnect succeeded and we are connected to the server! // send a welcome message SendMessageToServer("/say What? My name is..."); // start waiting for messages from the server SocketGlobals.AsyncReceiveState mReceiveState = new SocketGlobals.AsyncReceiveState(); mReceiveState.Socket = mState.Socket; mReceiveState.Socket.BeginReceive(mReceiveState.Buffer, 0, SocketGlobals.gBufferSize, SocketFlags.None, new AsyncCallback(ServerMessageReceived), mReceiveState); } catch (Exception ex) { // at this point, the EndConnect failed and we are NOT connected to the server! MessageBox.Show("Connect error: " + ex.Message); } }
/// <summary> /// Fires right when a client is connected to the server. /// </summary> /// <param name="ar"></param> /// <remarks></remarks> public void ConnectToServerCompleted(IAsyncResult ar) { // get the async state object which was returned by the async beginconnect method SocketGlobals.AsyncSendState mState = (SocketGlobals.AsyncSendState)ar.AsyncState; // end the async connection request so we can check if we are connected to the server try { // call the EndConnect method which will succeed or throw an error depending on the result of the connection mState.Socket.EndConnect(ar); // at this point, the EndConnect succeeded and we are connected to the server! // send a welcome message SendMessageToServer("/say What? My name is..."); // start waiting for messages from the server SocketGlobals.AsyncReceiveState mReceiveState = new SocketGlobals.AsyncReceiveState(); mReceiveState.Socket = mState.Socket; mReceiveState.Socket.BeginReceive(mReceiveState.Buffer, 0, SocketGlobals.gBufferSize, SocketFlags.None, new AsyncCallback(ServerMessageReceived), mReceiveState); } catch (Exception ex) { // at this point, the EndConnect failed and we are NOT connected to the server! MessageBox.Show("Connect error: " + ex.Message); } }
public void ServerMessageReceived(IAsyncResult ar) { // get the async state object from the async BeginReceive method SocketGlobals.AsyncReceiveState mState = (SocketGlobals.AsyncReceiveState) ar.AsyncState; // call EndReceive which will give us the number of bytes received int numBytesReceived = 0; numBytesReceived = mState.Socket.EndReceive(ar); // determine if this is the first data received if (mState.ReceiveSize == 0) { // this is the first data recieved, so parse the receive size which is encoded in the first four bytes of the buffer mState.ReceiveSize = BitConverter.ToInt32(mState.Buffer, 0); // write the received bytes thus far to the packet data stream mState.PacketBufferStream.Write(mState.Buffer, 4, numBytesReceived - 4); } else { // write the received bytes thus far to the packet data stream mState.PacketBufferStream.Write(mState.Buffer, 0, numBytesReceived); } // increment the total bytes received so far on the state object mState.TotalBytesReceived += numBytesReceived; // check for the end of the packet // bytesReceived = Carcassonne.Library.PacketBufferSize Then if (mState.TotalBytesReceived < mState.ReceiveSize) { // ## STILL MORE DATA FOR THIS PACKET, CONTINUE RECEIVING ## // the TotalBytesReceived is less than the ReceiveSize so we need to continue receiving more data for this packet mState.Socket.BeginReceive(mState.Buffer, 0, SocketGlobals.gBufferSize, SocketFlags.None, new AsyncCallback(ServerMessageReceived), mState); } else { // ## FINAL DATA RECEIVED, PARSE AND PROCESS THE PACKET ## // the TotalBytesReceived is equal to the ReceiveSize, so we are done receiving this Packet...parse it! System.Runtime.Serialization.Formatters.Binary.BinaryFormatter mSerializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); // rewind the PacketBufferStream so we can de-serialize it mState.PacketBufferStream.Position = 0; // de-serialize the PacketBufferStream which will give us an actual Packet object mState.Packet = (string) mSerializer.Deserialize(mState.PacketBufferStream); // parse the complete message that was received from the server ParseReceivedServerMessage(mState.Packet, mState.Socket); // call BeginReceive again, so we can start receiving another packet from this client socket SocketGlobals.AsyncReceiveState mNextState = new SocketGlobals.AsyncReceiveState(); mNextState.Socket = mState.Socket; mNextState.Socket.BeginReceive(mNextState.Buffer, 0, SocketGlobals.gBufferSize, SocketFlags.None, new AsyncCallback(ServerMessageReceived), mNextState); } }