Exemple #1
0
        void OnDisconnectedInternal(TCPConnection connection)
        {
            lock (this)
            {
                if (nodeMap.ContainsKey(connection.IP))
                {
                    ComNode node = nodeMap[connection.IP];
                    nodeMap.Remove(connection.IP);

                    if (OnDisconnected != null)
                    {
                        OnDisconnected(node);
                    }

                    Util.Log("Server:Disconnected");
                }
            }
        }
Exemple #2
0
        void OnConnectedInternal(TCPConnection connection)
        {
            if (connection == null)
            {
                return;
            }

            lock (this)
            {
                ComTCPNode node = new ComTCPNode(connection);

                nodeMap.Add(node.IP, node);

                connection.OnDisconnected = OnDisconnectedInternal;
                connection.OnPoll         = OnPoll;
                Util.Log("Server:Connected");
            }
        }
Exemple #3
0
        void OnConnectedInternal(string ip, TCPConnection connection)
        {
            if (connection != null)
            {
                serverNode = new ComTCPNode(connection);

                connection.OnDisconnected = OnDisconnectedInternal;
                connection.OnPoll         = OnPoll;

                Send((short)PreservedChannelId.Login, UserName);
                if (OnConnected != null)
                {
                    OnConnected(serverNode);
                }

                Util.Log("Client:Connected");
            }

            isConnecting = false;
        }
Exemple #4
0
        void OnDisconnectedInternal(TCPConnection connection)
        {
            IsConnected = false;
            if (OnDisconnected != null)
            {
                OnDisconnected(serverNode);
            }

            lock (serverNodeLocker)
            {
                previousServerNode = serverNode;
                serverNode         = null;
            }

            if (udpTerminal != null)
            {
                udpTerminal.ReceiveStop();
            }

            isDisconnecting = false;
        }
Exemple #5
0
        public async void Start()
        {
            IsActive = true;

            listener.Start();


            while (IsActive)
            {
                try
                {
                    TcpClient client = await listener.AcceptTcpClientAsync();

                    client.SendBufferSize    = connectionBufferSize;
                    client.ReceiveBufferSize = connectionBufferSize;
                    client.SendTimeout       = DefaultSendTimeoutMs;
                    client.ReceiveTimeout    = DefaultReceiveTimeoutMs;

                    TCPConnection connection = new TCPConnection(client, connectionBufferSize);

                    if (OnConnected != null)
                    {
                        OnConnected(connection);
                    }

                    connection.Start();
                }
                catch //(Exception e)
                {
                    if (OnConnected != null)
                    {
                        OnConnected(null);
                    }
                    //Util.Log(e.Message);
                }
            }
        }
Exemple #6
0
        public async Task Connect(string ip)
        {
            TcpClient client = new TcpClient();

            client.SendBufferSize    = connectionBufferSize;
            client.ReceiveBufferSize = connectionBufferSize;
            client.SendTimeout       = DefaultSendTimeoutMs;
            client.ReceiveTimeout    = DefaultReceiveTimeoutMs;

            try
            {
                Task con_task = client.ConnectAsync(ip, portNum);
                if (!con_task.Wait(connectTimeOutSec))
                {
                    client.Close();
                    throw new SocketException(10060); // 10060:WSAETIMEDOUT
                }
            }
            catch (SocketException e)
            {
                if (Global.SyncContext != null)
                {
                    Global.SyncContext.Post((state) => {
                        CallbackParam param = (CallbackParam)state;
                        if (OnConnected != null)
                        {
                            OnConnected(param.Ip, param.Connection);
                        }
                    }, new CallbackParam(ip, null));
                }
                else
                {
                    if (OnConnected != null)
                    {
                        OnConnected(ip, null);
                    }
                }
                //Util.Log("SocketException");
            }
            catch (AggregateException e)
            {
                if (e.InnerException is SocketException)
                {
                    if (Global.SyncContext != null)
                    {
                        Global.SyncContext.Post((state) => {
                            CallbackParam param = (CallbackParam)state;
                            if (OnConnected != null)
                            {
                                OnConnected(param.Ip, param.Connection);
                            }
                        }, new CallbackParam(ip, null));
                    }
                    else
                    {
                        if (OnConnected != null)
                        {
                            OnConnected(ip, null);
                        }
                    }
                    //Util.Log("AggregateException");
                }
            }

            TCPConnection connection = new TCPConnection(client, connectionBufferSize);

            if (Global.SyncContext != null)
            {
                Global.SyncContext.Post((state) => {
                    CallbackParam param = (CallbackParam)state;
                    if (OnConnected != null)
                    {
                        OnConnected(param.Ip, param.Connection);
                    }
                }, new CallbackParam(ip, connection));
            }
            else
            {
                if (OnConnected != null)
                {
                    OnConnected(ip, connection);
                }
            }
            await connection.Start();
        }
Exemple #7
0
 public CallbackParam(string ip, TCPConnection connection)
 {
     this.Ip = ip; this.Connection = connection;
 }
Exemple #8
0
 public ComTCPNode(TCPConnection connection)
     : base(connection.IP)
 {
     this.Connection = connection;
 }
Exemple #9
0
 public ComNode(TCPConnection connection)
 {
     this.Connection = connection;
 }
Exemple #10
0
 public ComSnowballNode(TCPConnection connection)
     : base((IPEndPoint)connection.Client.Client.RemoteEndPoint)
 {
     this.Connection = connection;
 }
Exemple #11
0
        async Task <bool> OnPoll(
            TCPConnection connection,
            NetworkStream nStream,
            byte[] receiveBuffer,
            BytePacker receivePacker,
            CancellationTokenSource cancelToken
            )
        {
            int   resSize   = 0;
            int   tmpSize   = 0;
            short channelId = 0;

            bool isRent = false;

            byte[] buffer = null;

            try
            {
                resSize = await ReadAsync(nStream, receiveBuffer, 2, cancelToken).ConfigureAwait(false);

                if (resSize != 0)
                {
                    receivePacker.Position = 0;
                    resSize = receivePacker.ReadShort();
#if DISABLE_CHANNEL_VARINT
                    tmpSize = await ReadAsync(nStream, receiveBuffer, 2, cancelToken).ConfigureAwait(false);

                    if (tmpSize == 0)
                    {
                        throw new EndOfStreamException();
                    }

                    receivePacker.Position = 0;
                    channelId = receivePacker.ReadShort();
#else
                    int s = 0;
                    channelId = VarintBitConverter.ToShort(nStream, out s);
#endif
                    tmpSize = await ReadAsync(nStream, receiveBuffer, resSize, cancelToken).ConfigureAwait(false);

                    if (tmpSize == 0)
                    {
                        throw new EndOfStreamException();
                    }

                    buffer = arrayPool.Rent(resSize);
                    if (buffer != null)
                    {
                        isRent = true;
                    }
                    else
                    {
                        buffer = new byte[resSize];
                        isRent = false;
                    }

                    Array.Copy(receiveBuffer, buffer, resSize);

                    //Util.Log("TCP:" + resSize);
                }
            }
            catch//(Exception e)
            {
                //Util.Log("TCP:" + e.Message);
                return(false);
            }

            if (resSize == 0)
            {
                return(false);
            }

            if (cancelToken.IsCancellationRequested)
            {
                return(false);
            }

            if (syncContext != null)
            {
                syncContext.Post((state) =>
                {
                    if (cancelToken.IsCancellationRequested)
                    {
                        return;
                    }
                    CallbackParam param = (CallbackParam)state;
                    OnReliableReceived(param.endPoint, param.channelId, param.buffer, param.size);
                    if (isRent)
                    {
                        arrayPool.Return(buffer);
                    }
                }, new CallbackParam((IPEndPoint)connection.Client.Client.RemoteEndPoint, channelId, buffer, resSize, isRent));
            }
            else
            {
                OnReliableReceived((IPEndPoint)connection.Client.Client.RemoteEndPoint, channelId, buffer, resSize);
            }

            return(true);
        }
Exemple #12
0
 public CallbackParam(TCPConnection connection)
 {
     this.Connection = connection;
 }