예제 #1
0
        private void readCallback(IAsyncResult ar)
        {
            try
            {
                netStream.EndRead(ar);
                NetworkMessage netMesasge = new NetworkMessageFormatter <NetworkMessage>().Deserialize(ar.AsyncState as byte[]);
                if (netMesasge != null)
                {
                    ReadCompleted?.Invoke(this, new NetworkMessageReaderReadCompletedEventArgs(netMesasge, TcpClient));
                }
                else
                {
                    ReadError?.Invoke(this, new NetworkMessageReaderReadErrorEventArgs(TcpClient, new ArgumentNullException("NetworkMessage is null")));
                }


                if (readLoop)
                {
                    ReadAsync(readLoop);
                }
            }
            catch (Exception ex)
            {
                ReadError?.Invoke(this, new NetworkMessageReaderReadErrorEventArgs(TcpClient, ex));
            }
        }
예제 #2
0
        public void WriteAsync(NetworkMessage netMessage)
        {
            try
            {
                TcpClient.SendBufferSize = NetworkMessage.MAX_DATA_SIZE_IN_BYTES;
                byte[] buffer = new NetworkMessageFormatter <NetworkMessage>().Serialize(netMessage);

                netStream = TcpClient.GetStream();
                netStream.BeginWrite(buffer, 0, buffer.Length, writeCallback, netMessage);
            }
            catch (Exception ex)
            {
                WriteError?.Invoke(this, new NetworkMessageWriterWriteErrorEventArgs(netMessage, TcpClient, ex));
            }
        }