Пример #1
0
 public bool IsConnected()
 {
     if (Socket != null)
     {
         return(Socket.IsConnected());
     }
     return(false);
 }
Пример #2
0
        public void Load()
        {
            //Dimlibs.Instance.Logger.Info("Is chat socket open? " + IsChatOpen());

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

            try
            {
                if (!Program.LaunchParameters.ContainsKey("-chat"))
                {
                    TcpListener listener = new TcpListener(IPAddress.Any, _port);
                    listener.Start();
                    ThreadPool.QueueUserWorkItem(InitializeChatRelay, listener);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            while (true)
            {
                if (_socket != null && _socket.IsConnected())
                {
                    break;
                }
            }



            if (Main.netMode == 2)
            {
                On.Terraria.Net.NetManager.SendToServer += NetManagerOnSendToServer;
                _socket = new TcpSocket();
                Netplay.SetRemoteIP("127.0.0.1");
                _socket.Connect(new Terraria.Net.TcpAddress(Netplay.ServerIP, _port));
                connection            = new RemoteServer();
                connection.Socket     = _socket;
                connection.ReadBuffer = new byte[ushort.MaxValue];
            }
            else
            {
                On.Terraria.Net.NetManager.SendToClient += NetManagerOnSendToClient;
                _socket = new TcpSocket();
                Netplay.SetRemoteIP("127.0.0.1");
                _socket.Connect(new Terraria.Net.TcpAddress(Netplay.ServerIP, _port));
                connection            = new RemoteServer();
                connection.Socket     = _socket;
                connection.ReadBuffer = new byte[ushort.MaxValue];
            }

            Dimlibs.Instance.Logger.Info("Chat is initialized and ready to go");
        }
Пример #3
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();
                            }
                        }
                    }
                }
            }
        }
Пример #4
0
        private void ServerLoop(object context)
        {
            TcpListener listener       = context as TcpListener;
            TcpClient   client         = listener.AcceptTcpClient();
            TcpSocket   terrariaSocket = new TcpSocket(client);
            ISocket     socket         = terrariaSocket as ISocket;

            Console.WriteLine("server loop started");

            RemoteServer server = new RemoteServer();


            server.Socket = terrariaSocket;
            while (false)
            {
                try
                {
                    if (socket.IsConnected())
                    {
                        if (socket.IsDataAvailable())
                        {
                            client.GetStream().Flush();
                            byte[] data = new byte[ushort.MaxValue];
                            using (MemoryStream stream = new MemoryStream(data))
                            {
                                BinaryWriter writer = new BinaryWriter(stream);
                                socket.AsyncSend(writer.BaseStream.ReadAllBytes(), 0, 256, new SocketSendCallback(Netplay.Connection.ClientWriteCallBack), null);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
            listener.Stop();
            Console.WriteLine("server loop stopped");
        }
Пример #5
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);
        }