コード例 #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);
            }
        }
コード例 #2
0
 internal void Acquire()
 {
     Reset();
     BusyTime = DateTime.Now;
     IdleTime = DateTime.MaxValue;
     Status   = ClientSocketStatus.Busy;
 }
コード例 #3
0
ファイル: ClientSocket.cs プロジェクト: yhhno/Adhesive
 internal void Acquire()
 {
     Reset();
     BusyTime = DateTime.Now;
     IdleTime = DateTime.MaxValue;
     Status = ClientSocketStatus.Busy;
 }
コード例 #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;
 }
コード例 #5
0
ファイル: GameLoader.cs プロジェクト: pdm1996/Fragsurf-2
        private void Socket_OnStatusChanged(ClientSocketStatus status, string reason = null)
        {
            if (Game.IsHost)
            {
                return;
            }

            if (status == ClientSocketStatus.Disconnected)
            {
                if (reason == DenyReason.MapChange.ToString())
                {
                    BeginRetry();
                }
            }
        }
コード例 #6
0
ファイル: ClientSocket.cs プロジェクト: yhhno/Adhesive
 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();
     }
 }
コード例 #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);
        }
コード例 #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();
     }
 }
コード例 #9
0
 internal void Release()
 {
     BusyTime = DateTime.MaxValue;
     IdleTime = DateTime.Now;
     Status   = ClientSocketStatus.Idle;
 }
コード例 #10
0
 protected virtual void OnClientStatusChanged(ClientSocketStatus status, string reason)
 {
 }
コード例 #11
0
ファイル: ClientSocket.cs プロジェクト: yhhno/Adhesive
 internal void Release()
 {
     BusyTime = DateTime.MaxValue;
     IdleTime = DateTime.Now;
     Status = ClientSocketStatus.Idle;
 }
コード例 #12
0
ファイル: ClientSocket.cs プロジェクト: yhhno/Adhesive
        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);
            }
        }
コード例 #13
0
ファイル: ClientSocket.cs プロジェクト: yhhno/Adhesive
        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;
        }
コード例 #14
0
ファイル: ClientSocket.cs プロジェクト: yhhno/Adhesive
 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;
 }