コード例 #1
0
 public void WiteTo(MessageStream stream)
 {
     byte[] bytes = BitConverter.GetBytes(SizeInBytes);
     stream.Write(bytes, 0, 4);
     bytes = BitConverter.GetBytes(Type);
     stream.Write(bytes, 0, 4);
 }
コード例 #2
0
ファイル: Worker.cs プロジェクト: TagsRocks/skill
        private void CloseConnection()
        {
            IsConnected = false;
            if (this._InStream != null)
            {
                this._InStream.Close();
            }
            if (this._LargeInStream != null)
            {
                this._LargeInStream.Close();
            }
            if (this._OutStream != null)
            {
                this._OutStream.Close();
            }
            if (this.Socket != null)
            {
                this.Socket.Close();
            }

            this._OutStream     = null;
            this._InStream      = null;
            this._LargeInStream = null;
            this.Socket         = null;

            OnClosed();
        }
コード例 #3
0
 public override void ReadData(MessageStream stream)
 {
     if (stream.ReadBoolean())
     {
         Text = stream.ReadString();
     }
 }
コード例 #4
0
 public void ReadFrom(MessageStream stream)
 {
     byte[] bytes = new byte[4];
     stream.Read(bytes, 0, 4);
     SizeInBytes = BitConverter.ToInt32(bytes, 0);
     stream.Read(bytes, 0, 4);
     this.Type = BitConverter.ToInt32(bytes, 0);
 }
コード例 #5
0
 public override void WriteData(MessageStream stream)
 {
     if (string.IsNullOrEmpty(Text))
     {
         stream.Write(false);
     }
     else
     {
         stream.Write(true);
         stream.Write(Text);
     }
 }
コード例 #6
0
ファイル: Worker.cs プロジェクト: TagsRocks/skill
 /// <summary>
 /// Start worker
 /// </summary>
 internal void Start(Socket socket)
 {
     IsConnected    = true;
     Exception      = null;
     this._InStream = new MessageStream(_DataBuffer)
     {
         CanRead = true
     };
     this._OutStream = new MessageStream(_DataBuffer.Length)
     {
         CanWrite = true
     };
     this.Socket = socket;
     WaitForData();
 }
コード例 #7
0
ファイル: Worker.cs プロジェクト: TagsRocks/skill
 /// <summary>
 /// handle recieved message
 /// </summary>
 /// <param name="stream">Stream that contains message data</param>
 /// <param name="header">Header of message</param>
 /// <returns>Message</returns>
 private Message HandleMessage(MessageStream stream, MessageHeader header)
 {
     if (_MessageTranslator != null)
     {
         stream.Flush();
         Message msg = _MessageTranslator.Translate(header.Type);
         if (msg != null)
         {
             msg.SizeInBytes = header.SizeInBytes;
             msg.ReadData(stream);
             OnMessage(msg);
         }
         else
         {
             Logger.LogWarning(string.Format("Can not translate message id : ", header.Type));
         }
         return(msg);
     }
     else
     {
         return(null);
     }
 }
コード例 #8
0
 public abstract void ReadData(MessageStream stream);
コード例 #9
0
 public abstract void WriteData(MessageStream stream);
コード例 #10
0
ファイル: Worker.cs プロジェクト: TagsRocks/skill
        // This the call back function which will be invoked when the socket
        // detects any client writing of data on the stream
        private void OnDataReceived(IAsyncResult asyn)
        {
            try
            {
                if (Socket == null || !IsConnected)
                {
                    return;
                }

                // Complete the BeginReceive() asynchronous call by EndReceive() method
                // which will return the number of characters written to the stream
                // by the client
                int dataLenght = Socket.EndReceive(asyn);

                if (dataLenght > 0)
                {
                    if (_MultipartMsg) // this should be the next part of a previous message
                    {
                        if (dataLenght > 0)
                        {
                            _LargeInStream.CanWrite = true;
                            _LargeInStream.Write(_DataBuffer, 0, dataLenght);
                            _LargeInStream.CanWrite = false;
                            this._DataRecieved     += dataLenght;
                        }
                    }
                    else // this is the first part of a message
                    {
                        if (dataLenght > MessageHeader.HeaderSize)
                        {
                            this._InHeader.ReadFrom(_DataBuffer);
                            this._DataRecieved = dataLenght - MessageHeader.HeaderSize;
                            if (this._InHeader.SizeInBytes > this._DataRecieved)
                            {
                                // the message has another part
                                _MultipartMsg = true;
                                if (_LargeInStream == null)
                                {
                                    _LargeInStream = new MessageStream(this._InHeader.SizeInBytes)
                                    {
                                        CanRead = true
                                    };
                                    Logger.LogWarning(string.Format("Size of message({0}) is greater than buffersize({1})", this._InHeader.SizeInBytes, _DataBuffer.Length));
                                }
                                else
                                {
                                    _LargeInStream.Seek(0, SeekOrigin.Begin);
                                }
                                _LargeInStream.CanWrite = true;
                                _LargeInStream.Write(_DataBuffer, MessageHeader.HeaderSize, _DataRecieved);
                                _LargeInStream.CanWrite = false;
                            }
                            else
                            {
                                _InStream.CanWrite = true;
                                _InStream.Seek(0, SeekOrigin.Begin);
                                _InStream.Write(_DataBuffer, MessageHeader.HeaderSize, _DataRecieved);
                                _InStream.CanWrite = false;
                            }
                        }
                    }

                    Message msg = null;

                    if (_MultipartMsg)
                    {
                        if (this._DataRecieved >= this._InHeader.SizeInBytes)
                        {
                            // this is the last part of message
                            _MultipartMsg = false;
                            _LargeInStream.Seek(0, SeekOrigin.Begin);
                            msg = HandleMessage(_LargeInStream, this._InHeader);
                            this._InHeader.SizeInBytes = 0;
                        }
                    }
                    else
                    {
                        _InStream.Seek(0, SeekOrigin.Begin);
                        msg = HandleMessage(_InStream, this._InHeader);
                        this._InHeader.SizeInBytes = 0;
                    }

                    if (msg != null && msg.Type == (int)MessageType.Disconnect)
                    {
                        CloseConnection();
                    }
                    else
                    {
                        // Continue the waiting for data on the Socket
                        WaitForData();
                    }
                }
            }
            catch (ObjectDisposedException ex)
            {
                Exception = ex;
                System.Diagnostics.Debugger.Log(0, "Errors", "Socket has been closed");
                Close();
            }
            catch (SocketException se)
            {
                if (se.ErrorCode == 10054) // Error code for Connection reset by peer
                {
                    string msg = string.Format("Client {0} Disconnected", Name);
                    Logger.LogMessage(msg);
                }
                else
                {
                    Logger.LogError(se);
                }
                Exception = se;
                Close();
            }
        }
コード例 #11
0
ファイル: DisconnectMessage.cs プロジェクト: TagsRocks/skill
 public override void ReadData(MessageStream stream)
 {
     stream.ReadBoolean();
 }
コード例 #12
0
ファイル: DisconnectMessage.cs プロジェクト: TagsRocks/skill
 public override void WriteData(MessageStream stream)
 {
     stream.Write(true);
 }