Esempio n. 1
0
        // Start waiting for data from the client
        private void WaitForData(Socket socket, string clientId)
        {
            try
            {
                if (workerCallback == null)
                {
                    // Specify the call back function which is to be
                    // invoked when there is any write activity by the
                    // connected client.
                    workerCallback = OnDataReceived;
                }

                SocketPacket socketPacket = new SocketPacket(socket, clientId);

                socket.BeginReceive(socketPacket.DataBuffer, 0, socketPacket.DataBuffer.Length, SocketFlags.None,
                                    workerCallback, socketPacket);
            }
            catch (SocketException se)
            {
#if DEBUG
                Debug.WriteLine("mbrc-log [SocketServer] 273: \t" + se);
#endif
                if (se.ErrorCode != 10053)
                {
#if DEBUG
                    ErrorHandler.LogError(se);
#endif
                }
                else
                {
                    EventBus.FireEvent(new MessageEvent(EventType.ActionClientDisconnected, string.Empty, clientId));
                }
            }
        }
Esempio n. 2
0
        // This is the call back function which will be invoked when the socket
        // detects any client writing of data on the stream
        private void OnDataReceived(IAsyncResult ar)
        {
            string clientId = String.Empty;

            try
            {
                SocketPacket socketData = (SocketPacket)ar.AsyncState;
                // Complete the BeginReceive() asynchronus call by EndReceive() method
                // which will return the number of characters written to the stream
                // by the client.

                clientId = socketData.ClientId;

                int    iRx   = socketData.MCurrentSocket.EndReceive(ar);
                char[] chars = new char[iRx + 1];

                System.Text.Decoder decoder = System.Text.Encoding.UTF8.GetDecoder();

                decoder.GetChars(socketData.DataBuffer, 0, iRx, chars, 0);
                if (chars.Length == 1 && chars[0] == 0)
                {
                    socketData.MCurrentSocket.Close();
                    socketData.MCurrentSocket.Dispose();
                    return;
                }
                String message = new string(chars);

                if (String.IsNullOrEmpty(message))
                {
                    return;
                }

#if DEBUG
                Debug.WriteLine(DateTime.Now.ToString(CultureInfo.InvariantCulture) + " : message Received : " + message);
#endif
                handler.ProcessIncomingMessage(message, socketData.ClientId);

                // Continue the waiting for data on the Socket.
                WaitForData(socketData.MCurrentSocket, socketData.ClientId);
            }
            catch (ObjectDisposedException)
            {
                EventBus.FireEvent(new MessageEvent(EventType.ActionClientDisconnected, string.Empty, clientId));
#if DEBUG
                Debug.WriteLine(DateTime.Now.ToString(CultureInfo.InvariantCulture) + " : OnDataReceived: Socket has been closed\n");
#endif
            }
            catch (SocketException se)
            {
                if (se.ErrorCode == 10054) // Error code for Connection reset by peer
                {
                    Socket deadSocket;
                    if (availableWorkerSockets.ContainsKey(clientId))
                    {
                        availableWorkerSockets.TryRemove(clientId, out deadSocket);
                    }
                    EventBus.FireEvent(new MessageEvent(EventType.ActionClientDisconnected, string.Empty, clientId));
                }
                else
                {
#if DEBUG
                    ErrorHandler.LogError(se);
#endif
                }
            }
        }
Esempio n. 3
0
        // Start waiting for data from the client
        private void WaitForData(Socket socket, string clientId)
        {
            try
            {
                if (workerCallback == null)
                {
                    // Specify the call back function which is to be
                    // invoked when there is any write activity by the
                    // connected client.
                    workerCallback = OnDataReceived;
                }

                SocketPacket socketPacket = new SocketPacket(socket, clientId);

                socket.BeginReceive(socketPacket.DataBuffer, 0, socketPacket.DataBuffer.Length, SocketFlags.None,
                                    workerCallback, socketPacket);
            }
            catch (SocketException se)
            {

            #if DEBUG
                Debug.WriteLine("mbrc-log [SocketServer] 273: \t" + se);
            #endif
                if (se.ErrorCode != 10053)
                {
            #if DEBUG
                    ErrorHandler.LogError(se);
            #endif
                }
                else
                {
                    EventBus.FireEvent(new MessageEvent(EventType.ActionClientDisconnected, string.Empty, clientId));
                }
            }
        }