Example #1
0
        private void HandshakeThread(Socket sckRead)
        {
            try
            {
                using (var h = DisposeHandle.Get(sckRead))
                    using (NetworkStream connectionStream = new NetworkStream(sckRead, false))
                    {
                        int nTp = connectionStream.ReadByte();

                        if (nTp != (int)NetworkMessageType.HANDSHAKE)
                        {
                            //Log.LogWriteLine("Invalid incoming connection message (expecting handshake): type {0} {1}", nTp, (MessageType)nTp);

                            sckRead.Close();
                            throw new Exception("Invalid incoming connection message (expecting handshake): type " + nTp + " " + (NetworkMessageType)nTp);
                        }

                        Handshake info = Serializer.Deserialize <Handshake>(Serializer.DeserializeChunk(connectionStream));

                        // swap
                        OverlayEndpoint remote = info.local;
                        OverlayEndpoint local  = info.remote;

                        info.remote = remote;
                        info.local  = local;

                        // read extra information
                        if (info.hasExtraInfo)
                        {
                            info.ExtraInfo = Serializer.DeserializeChunk(connectionStream);
                        }

                        processConnection.Invoke(info, sckRead);
                        h.Disengage();
                    }
            }
            catch (Exception e)
            {
                Log.Console("Error while processing handshake: {0}", e.Message);
                //onHandshakeError(e);
                throw;
            }
        }
Example #2
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");
            }
        }