Ejemplo n.º 1
0
        public void SendMessage(NetworkMessage nm)
        {
            if (IsClosed)
            {
                throw new NodeException("SendMessage: node is disconnected " + FullDescription());
            }
            if (writerStatus != WriteStatus.WRITING)
            {
                ConnectAsync();
            }

            SocketWriterMessage swm = new SocketWriterMessage(nm.ms);

            swm.delay = nm.delay;

            //string sentMsg = nm.ToString();
            //if (MasterFileLog.LogLevel > 2)
            //    sentMsg += new ChunkDebug(swm.message, Serializer.SizeSize).GetData() + "\n\n";

            //Log.EntryVerbose(LogW, nm.GetLogData());
            nm.LogData(LogW);

            writer.SendMessage(swm);

            UpdateUseTime();
        }
Ejemplo n.º 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);
                }
            }
        }
Ejemplo n.º 3
0
 public void SendMessage(SocketWriterMessage swm)
 {
     bcMessages.Add(swm);
 }