コード例 #1
0
ファイル: SocketServer.cs プロジェクト: omcdev/blockchain
        private void processConnect(IAsyncResult ar)
        {
            try
            {
                tcpClientConnect.Set();
                TcpListener listener = (TcpListener)ar.AsyncState;
                TcpClient   client   = listener.EndAcceptTcpClient(ar);
                client.ReceiveBufferSize = m_receiveBufferSize;

                var             stream = client.GetStream();
                TcpReceiveState state  = new TcpReceiveState();
                state.Client  = client;
                state.Address = client.Client.RemoteEndPoint.ToString();
                state.Stream  = stream;
                state.Buffer  = new byte[m_receiveBufferSize];

                Interlocked.Increment(ref ConnectCount);
                if (this.ReceivedMinerConnectionAction != null)
                {
                    if (ReceivedMinerConnectionAction(state, true))
                    {
                        state.Stream.BeginRead(state.Buffer, 0, state.Buffer.Length,
                                               new AsyncCallback(processReceive), state);
                    }
                }

                tcpClientConnect.WaitOne();
            }
            catch (Exception ex)
            {
                LogHelper.Error("error on processConnect", ex);
            }
            finally
            {
                this.startAccept();
            }
        }
コード例 #2
0
ファイル: SocketServer.cs プロジェクト: omcdev/blockchain
        private void processReceive(IAsyncResult ar)
        {
            TcpReceiveState state = (TcpReceiveState)ar.AsyncState;

            try
            {
                //if (state.Stream == null || !state.Stream.CanRead)
                //{
                //    state.Buffer = new byte[m_receiveBufferSize];
                //    state.Stream.BeginRead(state.Buffer, 0, state.Buffer.Length, new AsyncCallback(processReceive), state);
                //}

                int numberOfBytesRead = state.Stream.EndRead(ar);

                if (numberOfBytesRead > 0)
                {
                    //LogHelper.Debug($"Readed {numberOfBytesRead} byte data from {state.Address}");
                    var buffer = new byte[numberOfBytesRead];
                    Array.Copy(state.Buffer, 0, buffer, 0, buffer.Length);
                    //this.receivedDataQueue.Enqueue(new KeyValuePair<TcpReceiveState, byte[]>(state, buffer));
                    var         commandDataList = new List <byte[]>();
                    var         index           = 0;
                    List <byte> bytes           = null;

                    while (index < buffer.Length)
                    {
                        if (bytes == null)
                        {
                            if ((index + 3) < buffer.Length &&
                                buffer[index] == PoolCommand.DefaultPrefixBytes[0] &&
                                buffer[index + 1] == PoolCommand.DefaultPrefixBytes[1] &&
                                buffer[index + 2] == PoolCommand.DefaultPrefixBytes[2] &&
                                buffer[index + 3] == PoolCommand.DefaultPrefixBytes[3])
                            {
                                bytes = new List <byte>();
                                bytes.AddRange(PoolCommand.DefaultPrefixBytes);
                                index += 4;
                            }
                            else
                            {
                                index++;
                            }
                        }
                        else
                        {
                            if ((index + 3) < buffer.Length &&
                                buffer[index] == PoolCommand.DefaultSuffixBytes[0] &&
                                buffer[index + 1] == PoolCommand.DefaultSuffixBytes[1] &&
                                buffer[index + 2] == PoolCommand.DefaultSuffixBytes[2] &&
                                buffer[index + 3] == PoolCommand.DefaultSuffixBytes[3])
                            {
                                bytes.AddRange(PoolCommand.DefaultSuffixBytes);
                                commandDataList.Add(bytes.ToArray());
                                bytes = null;

                                index += 4;
                            }
                            else
                            {
                                bytes.Add(buffer[index]);
                                index++;
                            }
                        }
                    }

                    if (this.ReceivedCommandAction != null)
                    {
                        foreach (var data in commandDataList)
                        {
                            try
                            {
                                var cmd = PoolCommand.ConvertBytesToMessage(data);
                                if (cmd != null)
                                {
                                    HeartbeatCommand.UpdateHeartTime(state);
                                    this.ReceivedCommandAction(state, cmd);
                                }
                            }
                            catch (Exception ex)
                            {
                                LogHelper.Error("Error occured on deserialize messgae: " + ex.Message, ex);
                            }
                        }
                    }
                }

                state.Buffer = new byte[m_receiveBufferSize];
                state.Stream.BeginRead(state.Buffer, 0, state.Buffer.Length,
                                       new AsyncCallback(processReceive), state);
            }
            catch (Exception ex)
            {
                LogHelper.Error("Error occured on receive messgae: " + ex.Message, ex);
                this.CloseSocket(state);
            }
        }