public void ReceiveMessage(Socket sourceSocket, Action<string> messageReceivedCallback)
 {
     ReceiveState receiveState = new ReceiveState() {
         SourceSocket = sourceSocket,
         MessageReceivedCallback = messageReceivedCallback
     };
     this.ReceiveInternal(receiveState, 4);
 }
Example #2
0
        public void ReceiveMessage(Socket sourceSocket, Action <string> messageReceivedCallback)
        {
            ReceiveState receiveState = new ReceiveState()
            {
                SourceSocket            = sourceSocket,
                MessageReceivedCallback = messageReceivedCallback
            };

            this.ReceiveInternal(receiveState, 4);
        }
Example #3
0
        private void ReceiveCallback(IAsyncResult asyncResult)
        {
            ReceiveState receiveState = (ReceiveState)asyncResult.AsyncState;
            var          sourceSocket = receiveState.SourceSocket;
            var          data         = receiveState.Data;
            var          bytesRead    = 0;

            try {
                bytesRead = sourceSocket.EndReceive(asyncResult);
            }
            catch (SocketException socketException) {
                Console.WriteLine("Socket send exception, ErrorCode: {0}", socketException.ErrorCode);
            }
            catch (Exception ex) {
                Console.WriteLine("Unknown socket send exception: {0}", ex);
            }

            if (bytesRead > 0)
            {
                if (receiveState.MessageSize == null)
                {
                    receiveState.MessageSize = GetMessageLength(receiveState.Buffer);
                    int size = receiveState.MessageSize <= ReceiveState.BufferSize
                        ? receiveState.MessageSize.Value : ReceiveState.BufferSize;
                    this.ReceiveInternal(receiveState, size);
                }
                else
                {
                    for (int index = 0; index < bytesRead; index++)
                    {
                        data.Add(receiveState.Buffer[index]);
                    }
                    if (receiveState.Data.Count < receiveState.MessageSize.Value)
                    {
                        int remainSize = receiveState.MessageSize.Value - receiveState.Data.Count;
                        int size       = remainSize <= ReceiveState.BufferSize ? remainSize : ReceiveState.BufferSize;
                        this.ReceiveInternal(receiveState, size);
                    }
                    else
                    {
                        receiveState.MessageReceivedCallback(Encoding.ASCII.GetString(data.ToArray()));
                        receiveState.MessageSize = null;
                        receiveState.Data.Clear();
                        this.ReceiveInternal(receiveState, 4);
                    }
                }
            }
        }
 private IAsyncResult ReceiveInternal(ReceiveState receiveState, int size)
 {
     return(receiveState.SourceSocket.BeginReceive(receiveState.Buffer, 0, size, 0, ReceiveCallback, receiveState));
 }
 private void ReceiveInternal(ReceiveState receiveState, int size)
 {
     receiveState.SourceSocket.BeginReceive(receiveState.Buffer, 0, size, 0, this.ReceiveCallback, receiveState);
 }
Example #6
0
 private void ReceiveInternal(ReceiveState receiveState, int size)
 {
     receiveState.SourceSocket.BeginReceive(receiveState.Buffer, 0, size, 0, this.ReceiveCallback, receiveState);
 }