예제 #1
0
 // For Unittest and debugging.
 public TcpAdapter(Stream networkStream, IMessageBytesFiller messageByteFiller)
 {
     this.networkStream  = networkStream;
     sendingQueue        = new ConcurrentQueue <NetworkMessage>();
     receivingQueue      = new ConcurrentQueue <NetworkMessage>();
     sendingThread       = new Thread(new ThreadStart(this.Sending));
     receivingThread     = new Thread(new ThreadStart(this.Receiving));
     messageBytesFillers = new List <IMessageBytesFiller>()
     {
         messageByteFiller
     };
     this.SkipSsl = true;
 }
예제 #2
0
        private void Receiving()
        {
            try {
                // Waiting for sendingThread to establish the connnection.
                while (running && error == "" && networkStream == null)
                {
                    Thread.Sleep(0);
                }

                IMessageBytesFiller filler = null;
                while (running && error == "")
                {
                    if (filler == null)
                    {
                        ushort eventType = BytesUtils.ReadUInt16(networkStream);
                        // Find filler.
                        foreach (var eachFiller in messageBytesFillers)
                        {
                            if (eachFiller.EventType() == eventType)
                            {
                                filler = eachFiller;
                            }
                        }
                        if (filler == null)
                        {
                            throw new Exception("Could not find a filler for " + eventType.ToString());
                        }

                        // Add the event type into the filler.
                        var raw = BitConverter.GetBytes(eventType);
                        filler.Fill(raw[0]);
                        filler.Fill(raw[1]);
                    }
                    else
                    {
                        byte   b      = BytesUtils.ReadByte(networkStream);
                        byte[] buffer = filler.Fill(b);
                        if (buffer != null)
                        {
                            receivingQueue.Enqueue(SerializationUtils.DeserializeResponse(buffer));
                            // A new filler will be picked for the next message.
                            filler = null;
                        }
                    }
                }
            } catch (Exception e) {
                this.error = e.ToString();
                throw e;
            }
        }