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); } }
internal void Acquire() { Reset(); BusyTime = DateTime.Now; IdleTime = DateTime.MaxValue; Status = ClientSocketStatus.Busy; }
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; }
private void Socket_OnStatusChanged(ClientSocketStatus status, string reason = null) { if (Game.IsHost) { return; } if (status == ClientSocketStatus.Disconnected) { if (reason == DenyReason.MapChange.ToString()) { BeginRetry(); } } }
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(); } }
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); }
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(); } }
internal void Release() { BusyTime = DateTime.MaxValue; IdleTime = DateTime.Now; Status = ClientSocketStatus.Idle; }
protected virtual void OnClientStatusChanged(ClientSocketStatus status, string reason) { }
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; }
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; }