Exemplo n.º 1
0
        internal void Write(byte[] data, int offset, int length)
        {
            if (socket == null)
            {
                LocalLoggingService.Error("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientSocket", "Write",
                                          string.Format("Write 发送时没有获取到socket {0}", Endpoint.ToString()));
                return;
            }

            SocketError status;

            try
            {
                socket.Send(data, offset, length, SocketFlags.None, out status);

                if (status != SocketError.Success)
                {
                    Status = ClientSocketStatus.Error;
                    LocalLoggingService.Error("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientSocket", "Write",
                                              string.Format("Write 发送数据到 {0} 出错,错误为:{1}", Endpoint.ToString(), status.ToString()));
                    highLevelErrorAction(this);
                }
            }
            catch (Exception ex)
            {
                Status = ClientSocketStatus.Error;
                LocalLoggingService.Error("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientSocket", "Write", string.Format("发送数据到 {0} 出错", Endpoint.ToString()), ex.ToString());
                highLevelErrorAction(this);
            }
        }
Exemplo n.º 2
0
 internal void Acquire()
 {
     Reset();
     BusyTime = DateTime.Now;
     IdleTime = DateTime.MaxValue;
     Status   = ClientSocketStatus.Busy;
 }
Exemplo n.º 3
0
 internal void Acquire()
 {
     Reset();
     BusyTime = DateTime.Now;
     IdleTime = DateTime.MaxValue;
     Status = ClientSocketStatus.Busy;
 }
Exemplo n.º 4
0
 internal ClientSocket(Action <ClientSocket> disposeAction, Action <ClientSocket> lowlevelErrorAction, Action <ClientSocket> highLevelErrorAction, int sendTimeout, int receiveTimeout, string ip, int port)
 {
     this.disposeAction        = disposeAction;
     this.lowlevelErrorAction  = lowlevelErrorAction;
     this.highLevelErrorAction = highLevelErrorAction;
     Endpoint              = new IPEndPoint(IPAddress.Parse(ip), port);
     socket                = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     socket.NoDelay        = true;
     socket.SendTimeout    = sendTimeout;
     socket.ReceiveTimeout = receiveTimeout;
     CreateTime            = DateTime.Now;
     IdleTime              = DateTime.MaxValue;
     BusyTime              = DateTime.MaxValue;
     Status                = ClientSocketStatus.Idle;
 }
Exemplo n.º 5
0
        private void Socket_OnStatusChanged(ClientSocketStatus status, string reason = null)
        {
            if (Game.IsHost)
            {
                return;
            }

            if (status == ClientSocketStatus.Disconnected)
            {
                if (reason == DenyReason.MapChange.ToString())
                {
                    BeginRetry();
                }
            }
        }
Exemplo n.º 6
0
 internal void Destroy()
 {
     Status = ClientSocketStatus.Destroy;
     if (stream != null)
         stream.Close();
     if (socket != null && socket.Connected)
     {
         try
         {
             LocalLoggingService.Info("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientSocket", "Destroy",
                 string.Format("Destroy socket {0}", Endpoint.ToString()));
             socket.Shutdown(SocketShutdown.Both);
         }
         catch (Exception ex)
         {
             LocalLoggingService.Error("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientSocket", "Destroy", string.Format("Destroy socket {0} 的时候出现异常", Endpoint.ToString()), ex);
         }
         socket.Close();
     }
 }
Exemplo n.º 7
0
        internal byte[] Read(int count)
        {
            if (socket == null)
            {
                return(null);
            }


            var buffer = new byte[count];
            int offset = 0;

            int read       = 0;
            int shouldRead = count;

            while (read < count)
            {
                try
                {
                    int currentRead = stream.Read(buffer, offset, shouldRead);
                    if (currentRead < 1)
                    {
                        continue;
                    }

                    read       += currentRead;
                    offset     += currentRead;
                    shouldRead -= currentRead;
                }
                catch (Exception ex)
                {
                    LocalLoggingService.Error("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientSocket", "Read", string.Format("从 {0} 接受数据出错", Endpoint.ToString()), ex.ToString());

                    lowlevelErrorAction(this);
                    Status = ClientSocketStatus.Error;
                    return(null);
                }
            }

            return(buffer);
        }
Exemplo n.º 8
0
 internal void Destroy()
 {
     Status = ClientSocketStatus.Destroy;
     if (stream != null)
     {
         stream.Close();
     }
     if (socket != null && socket.Connected)
     {
         try
         {
             LocalLoggingService.Info("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientSocket", "Destroy",
                                      string.Format("Destroy socket {0}", Endpoint.ToString()));
             socket.Shutdown(SocketShutdown.Both);
         }
         catch (Exception ex)
         {
             LocalLoggingService.Error("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientSocket", "Destroy", string.Format("Destroy socket {0} 的时候出现异常", Endpoint.ToString()), ex);
         }
         socket.Close();
     }
 }
Exemplo n.º 9
0
 internal void Release()
 {
     BusyTime = DateTime.MaxValue;
     IdleTime = DateTime.Now;
     Status   = ClientSocketStatus.Idle;
 }
Exemplo n.º 10
0
 protected virtual void OnClientStatusChanged(ClientSocketStatus status, string reason)
 {
 }
Exemplo n.º 11
0
 internal void Release()
 {
     BusyTime = DateTime.MaxValue;
     IdleTime = DateTime.Now;
     Status = ClientSocketStatus.Idle;
 }
Exemplo n.º 12
0
        internal void Write(byte[] data, int offset, int length)
        {
            if (socket == null)
            {
                LocalLoggingService.Error("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientSocket", "Write",
                    string.Format("Write 发送时没有获取到socket {0}", Endpoint.ToString()));
                return;
            }

            SocketError status;

            try
            {

                socket.Send(data, offset, length, SocketFlags.None, out status);

                if (status != SocketError.Success)
                {
                    Status = ClientSocketStatus.Error;
                    LocalLoggingService.Error("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientSocket", "Write",
                        string.Format("Write 发送数据到 {0} 出错,错误为:{1}", Endpoint.ToString(), status.ToString()));
                    highLevelErrorAction(this);
                }
            }
            catch (Exception ex)
            {
                Status = ClientSocketStatus.Error;
                LocalLoggingService.Error("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientSocket", "Write", string.Format("发送数据到 {0} 出错", Endpoint.ToString()), ex.ToString());
                highLevelErrorAction(this);
            }
        }
Exemplo n.º 13
0
        internal byte[] Read(int count)
        {
            if (socket == null) return null;


            var buffer = new byte[count];
            int offset = 0;

            int read = 0;
            int shouldRead = count;

            while (read < count)
            {
                try
                {
                    int currentRead = stream.Read(buffer, offset, shouldRead);
                    if (currentRead < 1)
                        continue;

                    read += currentRead;
                    offset += currentRead;
                    shouldRead -= currentRead;
                }
                catch (Exception ex)
                {
                    LocalLoggingService.Error("{0} {1} {2} {3}", DistributedServerConfiguration.ModuleName, "ClientSocket", "Read", string.Format("从 {0} 接受数据出错", Endpoint.ToString()), ex.ToString());

                    lowlevelErrorAction(this);
                    Status = ClientSocketStatus.Error;
                    return null;
                }
            }

            return buffer;
        }
Exemplo n.º 14
0
 internal ClientSocket(Action<ClientSocket> disposeAction, Action<ClientSocket> lowlevelErrorAction, Action<ClientSocket> highLevelErrorAction, int sendTimeout, int receiveTimeout, string ip, int port)
 {
     this.disposeAction = disposeAction;
     this.lowlevelErrorAction = lowlevelErrorAction;
     this.highLevelErrorAction = highLevelErrorAction;
     Endpoint = new IPEndPoint(IPAddress.Parse(ip), port);
     socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     socket.NoDelay = true;
     socket.SendTimeout = sendTimeout;
     socket.ReceiveTimeout = receiveTimeout;
     CreateTime = DateTime.Now;
     IdleTime = DateTime.MaxValue;
     BusyTime = DateTime.MaxValue;
     Status = ClientSocketStatus.Idle;
 }