Exemple #1
0
        public bool HandleMessage(SocketAsyncEventArgs receiveSendEventArgs, DataHoldingUserToken receiveSendToken)
        {
            var  logger = LogManager.GetLogger(this.GetType());
            bool incomingTcpMessageIsReady = false;

            var currentChunk = new byte[receiveSendEventArgs.BytesTransferred];

            Array.Copy(receiveSendEventArgs.Buffer, currentChunk, receiveSendEventArgs.BytesTransferred);

            // Look for delimiter in bytes
            var delimiterPos = SearchBytes(currentChunk, receiveSendToken.messageDelimiter);

            if (delimiterPos < 0)
            {
                logger.Debug("No delimiter yet for token: " + receiveSendToken.TokenId);
                // No delimiter. We need to receive more data...
                // Append data to the token buffer
                receiveSendToken.theDataHolder.Append(receiveSendEventArgs.Buffer, receiveSendEventArgs.BytesTransferred);
                receiveSendToken.bytesAlreadyRead    += receiveSendEventArgs.BytesTransferred;
                receiveSendToken.receiveMessageOffset = 0;
                // receive more...
            }
            else
            {
                logger.Debug("Found the end of a message! Token: " + receiveSendToken.TokenId);
                receiveSendToken.theDataHolder.Append(receiveSendEventArgs.Buffer, delimiterPos); // read up until before the delimiter
                receiveSendToken.bytesAlreadyRead    += delimiterPos + receiveSendToken.messageDelimiter.Length - 1;
                receiveSendToken.receiveMessageOffset = delimiterPos + receiveSendToken.messageDelimiter.Length;
                incomingTcpMessageIsReady             = true;
            }

            return(incomingTcpMessageIsReady);
        }
        public static void CloseConnection(string name)
        {
            if (!_sockets.ContainsKey(name))
            {
                throw new Exception($"No connection with name {name} exists");
            }

            var sock = _sockets[name];

            var e = new SocketAsyncEventArgs();

            sock._bufferManager.SetBuffer(e);

            var bytes        = Encoding.UTF8.GetBytes("");
            var recSendToken = new DataHoldingUserToken(bytes, Guid.NewGuid().ToString(), null);

            recSendToken.CreateNewDataHolder();
            e.UserToken = recSendToken;
            sock.StartDisconnect(e);
            _sockets.Remove(name);
        }
        public void StartReceiving(Func <byte[], bool> onReceive, byte[] messageDelimiter)
        {
            var e = new SocketAsyncEventArgs();

            _bufferManager.SetBuffer(e);

            var recSendToken = new DataHoldingUserToken(messageDelimiter, Guid.NewGuid().ToString(), onReceive);

            //Create an object that we can write data to, and remove as an object
            //from the UserToken, if we wish.
            recSendToken.CreateNewDataHolder();

            e.UserToken = recSendToken;

            e.Completed += (sender, args) => {
                try
                {
                    LogManager.GetLogger(this.GetType()).Debug("IO_Completed method accessed with op: " + args.LastOperation);

                    // determine which type of operation just completed and call the associated handler
                    switch (args.LastOperation)
                    {
                    case SocketAsyncOperation.Connect:
                        LogManager.GetLogger(this.GetType()).Debug("IO_Completed method In Connect. NOT IMPLEMENTED!");
                        //ProcessConnect(e);
                        break;

                    case SocketAsyncOperation.Receive:
                        var recToken = (DataHoldingUserToken)e.UserToken;
                        LogManager.GetLogger(this.GetType()).Debug("IO_Completed method In Receive, id = " + recToken.TokenId);
                        ProcessReceive(args);

                        break;

                    case SocketAsyncOperation.Send:
                        var sendToken = (DataHoldingUserToken)e.UserToken;
                        LogManager.GetLogger(this.GetType()).Debug("IO_Completed method In Send, id = " + sendToken.TokenId + ". NOT IMPLEMENTED!");
                        //ProcessSend(e);
                        break;

                    case SocketAsyncOperation.Disconnect:
                        var disconnectToken = (DataHoldingUserToken)e.UserToken;
                        LogManager.GetLogger(this.GetType()).Debug("IO_Completed method In Disconnect, id = " + disconnectToken.TokenId + ". NOT IMPLEMENTED!");
                        //ProcessDisconnectAndCloseSocket(e);
                        break;

                    default:
                    {
                        var errorToken = (DataHoldingUserToken)e.UserToken;
                        LogManager.GetLogger(this.GetType()).Debug("Error in I/O Completed, id = " + errorToken.TokenId + ". NOT IMPLEMENTED!");
                        throw new ArgumentException("\r\nError in I/O Completed, id = " + errorToken.TokenId);
                    }
                    }
                }
                catch (Exception err)
                {
                    var errorToken = (DataHoldingUserToken)e.UserToken;
                    LogManager.GetLogger(this.GetType()).Error("EXCEPTION in I/O Completed, id = " + errorToken.TokenId + ".", err);
                }
            };

            _socket.ReceiveAsync(e);
        }