示例#1
0
 private void TryRead()
 {
     if (!_isReading)
     {
         try
         {
             if (Socket.IsDataAvailable())
             {
                 _isReading = true;
                 Socket.AsyncReceive(ReadBuffer, 0, ReadBuffer.Length, ServerReadCallBack);
             }
         }
         catch
         {
             PendingTermination = true;
         }
     }
 }
示例#2
0
        /// <summary>
        /// Initalize Server chat relay, possible message:
        /// First Byte : Send/Receive chat message
        /// Second Byte : From server/client
        /// Rest : The text from the chat
        /// </summary>
        /// <param name="server"></param>
        internal void InitializeChatRelay(object server)
        {
            TcpListener listener       = server as TcpListener;
            TcpClient   client         = listener.AcceptTcpClient();
            TcpSocket   terrariaSocket = new TcpSocket(client);
            ISocket     chatRelay      = terrariaSocket as ISocket;

            Dimlibs.Instance.Logger.Info("Chat initializing");

            while (true)
            {
                if (chatRelay.IsConnected())
                {
                    if (listener.Pending())
                    {
                        TcpClient  newClient = listener.AcceptTcpClient();
                        IPEndPoint endpoint  = newClient.Client.RemoteEndPoint as IPEndPoint;
                        if (endpoint.Address.Equals(IPAddress.Parse("127.0.0.1")))
                        {
                            localServer.Add(newClient);
                        }
                        else
                        {
                            otherPlayer.Add(newClient);
                        }
                    }

                    if (chatRelay.IsDataAvailable())
                    {
                        byte[] data = new byte[ushort.MaxValue];
                        _socket.AsyncReceive(data, 0, ushort.MaxValue, new SocketReceiveCallback(Netplay.Connection.ClientReadCallBack));
                        using (MemoryStream stream = new MemoryStream(data))
                        {
                            using (BinaryReader reader = new BinaryReader(stream))
                            {
                                byte playerID = reader.ReadByte();
                            }
                        }
                    }
                }
            }
        }
示例#3
0
        public bool Detect()
        {
            try
            {
                _socket = CreateSocket();

                _working = true;
                Log("start working");

                _socket.Connect(new TcpAddress(IPAddress.Loopback, TShock.Config.ServerPort));

                if (_socket.IsConnected())
                {
                    Log("connected");
                }
                else
                {
                    throw new SocketException(ConnectionRefusedCode);
                }
            }
            catch (SocketException ex)
            {
                if (ex.ErrorCode == ConnectionRefusedCode)
                {
                    Log("connection failed");
                }

                Log(ex.ToString());

                CloseSocket();

                return(true);
            }

            try
            {
                _socket.AsyncSend(_writeBuffer, 0, _versionStrLength, SendCallback);
                _socket.AsyncReceive(_readBuffer, 0, _readBuffer.Length, ReceiveCallback);

                for (var i = 0; i < 3; i++)
                {
                    if (!_working)
                    {
                        break;
                    }

                    Thread.Sleep(1000);
                }
            }
            catch (Exception ex)
            {
                Log("Unhandled exception in socket send & receive");
                Log(ex.ToString());
            }
            finally
            {
                CloseSocket();
            }

            return(_working || _status);
        }