Beispiel #1
0
        void ProcessThread()
        {
            try
            {
                using (Stream readStream = new NetworkStream(socketRead, true))
                {
                    while (true)
                    {
                        int bt = readStream.ReadByte();

                        if (bt == -1)
                        {
                            throw new IOException("End of stream");
                        }
                        else if ((NetworkMessageType)bt == NetworkMessageType.SOFT_DISCONNECT)
                        {
                            onSoftDisconnect.Invoke();

                            readStream.WriteByte((byte)NetworkMessageType.SOFT_DISCONNECT);

                            return;
                        }
                        else if ((NetworkMessageType)bt == NetworkMessageType.MESSAGE)
                        {
                            //Console.WriteLine("Message received: {0}", (MessageType)bt);

                            messageProcessor.Invoke(Serializer.DeserializeChunk(readStream));
                        }
                        else
                        {
                            throw new Exception(Log.StDump("Unexpected", bt, (NetworkMessageType)bt));
                        }
                    }
                }
            }
            catch (IOException ioe)
            {
                errorResponse.Invoke(ioe);
                //Log.LogWriteLine("SocketReader terminated");
            }
        }
Beispiel #2
0
        private void WritingThread()
        {
            using (Socket writeSocket = new Socket(
                       address.AddressFamily,
                       SocketType.Stream,
                       ProtocolType.Tcp))
            {
                try
                {
                    writeSocket.Connect(address);//, TimeSpan.FromSeconds(5));

                    // send handshake data
                    using (NetworkStream connectionStream = new NetworkStream(writeSocket, false))
                    {
                        connectionStream.WriteByte((byte)NetworkMessageType.HANDSHAKE);

                        Serializer.SendMemoryStream(connectionStream, Serializer.SerializeGet(info));

                        if (info.hasExtraInfo)
                        {
                            Serializer.SendMemoryStream(connectionStream, info.ExtraInfo);
                        }
                    }
                }
                catch (Exception e)
                {
                    errorResponse.Invoke(e, DisconnectType.WRITE_CONNECT_FAIL);
                    return;
                }

                try
                {
                    using (NetworkStream connectionStream = new NetworkStream(writeSocket, false))
                        while (true)
                        {
                            SocketWriterMessage swm = bcMessages.Take();
                            if (swm.type == SocketWriterMessageType.TERMINATE)
                            {
                                //Log.LogWriteLine("SocketWriter terminated gracefully");
                                return;
                            }
                            else if (swm.type == SocketWriterMessageType.MESSAGE)
                            {
                                if (swm.delay != TimeSpan.Zero)
                                {
                                    Thread.Sleep(swm.delay);
                                }

                                connectionStream.WriteByte((byte)NetworkMessageType.MESSAGE);

                                Serializer.SendMemoryStream(connectionStream, swm.message);
                            }
                            else if (swm.type == SocketWriterMessageType.SOFT_DISCONNECT)
                            {
                                if (!bcMessages.IsEmpty)
                                {
                                    continue;
                                }

                                connectionStream.WriteByte((byte)NetworkMessageType.SOFT_DISCONNECT);
                                int bt = connectionStream.ReadByte();
                                MyAssert.Assert(bt == (byte)NetworkMessageType.SOFT_DISCONNECT);
                                writeSocket.Close();

                                onSoftDisconnect.Invoke();

                                return;
                            }
                            else
                            {
                                throw new Exception(Log.StDump("Unexpected", swm.type));
                            }
                        }
                }
                catch (IOException ioe)
                {
                    errorResponse.Invoke(ioe, DisconnectType.WRITE);
                }
            }
        }