public static void CloseSock(ISocket socket, int timeout = 0) { if (socket.GetSocket().ProtocolType == ProtocolType.Tcp) socket.GetSocket().Shutdown(SocketShutdown.Both); if (timeout == 0) socket.Close(); else socket.Close(timeout); }
public static Tuple <int, EndPoint> ReceiveMessage(ISocket socket, Buffer buffer, SocketCommunicationTypes type = SocketCommunicationTypes.Blocking, MessageThreadCallback callback = null) { if (type == SocketCommunicationTypes.Blocking) { switch (socket.GetSocket().ProtocolType) { case ProtocolType.Tcp: return(Tuple.Create(socket.GetSocket().Receive(Buffer.GetBufferRef(buffer)), socket.GetSocket().RemoteEndPoint)); case ProtocolType.Udp: EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); return(Tuple.Create(socket.GetSocket().ReceiveFrom(Buffer.GetBufferRef(buffer), ref remoteEndPoint), remoteEndPoint)); default: throw new InvalidOperationException("Socket must be of type TCP or UDP."); } } if (callback == null) { throw new ArgumentNullException(string.Format("{0}=null; You must provide a valid callback when using the NonBlocking type", "callback")); } new Thread(() => MessageReceiveThread(socket, buffer, callback)).Start(); return(Tuple.Create(-1, new IPEndPoint(-1, -1) as EndPoint)); //Return negative 1 as 0 bytes received is valid and we want an invalid value }
public static void CloseSock(ISocket socket, int timeout = 0) { if (socket.GetSocket().ProtocolType == ProtocolType.Tcp) { socket.GetSocket().Shutdown(SocketShutdown.Both); } if (timeout == 0) { socket.Close(); } else { socket.Close(timeout); } }
public static int SendMessage(ISocket socket, string ip, int port, Buffer buffer) { if (socket.GetSocket().ProtocolType == ProtocolType.Tcp) { throw new ConstraintException("Cannot call this method with a TCP socket. Call SendMessage(Socket, Buffer) instead."); } return(socket.SendMessage(ip, port, buffer)); }
public static int SendMessage(ISocket socket, Buffer buffer) { if (socket.GetSocket().ProtocolType == ProtocolType.Udp) { throw new InvalidOperationException("Cannot call this method with a UDP socket. Call SendMessage(Socket, string, int, Buffer) instead."); } return(socket.SendMessage(buffer)); }
public static Tuple<int, EndPoint> ReceiveMessage(ISocket socket, Buffer buffer, SocketCommunicationTypes type = SocketCommunicationTypes.Blocking, MessageThreadCallback callback = null) { if (type == SocketCommunicationTypes.Blocking) { switch (socket.GetSocket().ProtocolType) { case ProtocolType.Tcp: return Tuple.Create(socket.GetSocket().Receive(Buffer.GetBufferRef(buffer)), socket.GetSocket().RemoteEndPoint); case ProtocolType.Udp: EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); return Tuple.Create(socket.GetSocket().ReceiveFrom(Buffer.GetBufferRef(buffer), ref remoteEndPoint), remoteEndPoint); default: throw new ConstraintException("Socket must be of type TCP or UDP."); } } if (callback == null) throw new ArgumentNullException(string.Format("{0}=null; You must provide a valid callback when using the NonBlocking type", "callback")); new Thread(() => MessageReceiveThread(socket, buffer, callback)).Start(); return Tuple.Create(-1, new IPEndPoint(-1, -1) as EndPoint); //Return negative 1 as 0 bytes received is valid and we want an invalid value }
Socket AcceptClient(ISocket iSocket, Exception exception) { _logger.Debug($"Client connection Socket: {iSocket} Error: {exception}"); if (exception != null) { return(null); } return(iSocket.GetSocket()); }
public static int GetRemotePort(ISocket socket) { if (socket.GetSocket().ProtocolType == ProtocolType.Udp) { throw new ConstraintException("Cannot get remote IP Address of a UDP socket directly. It is returned from the ReceiveMessage as the second item in the Tuple<>"); } var socketEndPoint = (IPEndPoint)socket.GetRemoteEndPoint(); return(socketEndPoint.Port); }
// listen socket 에 의해 accept 가 되었을 때 호출된다. public void OnAccepted(ISocket sock) { Console.WriteLine("connected from {0}", sock.GetSocket().RemoteEndPoint.ToString()); //// client 에게 SC_ACCEPTED //SC_ACCEPTED_Packet packet = new SC_ACCEPTED_Packet(); //packet.AcceptStatus = 1; //PacketHelper.Send(sock, (Int16)SC_PacketType.SC_ACCEPTED, packet); }
public static IPAddress GetRemoteIpAddress(ISocket socket) { if (socket.GetSocket().ProtocolType == ProtocolType.Udp) { throw new InvalidOperationException("Cannot get remote IP Address of a UDP socket directly. It is returned from the ReceiveMessage as the second item in the Tuple<>"); } var socketEndPoint = (IPEndPoint)socket.GetRemoteEndPoint(); return(socketEndPoint.Address); }
private static void MessageReceiveThread(ISocket socket, Buffer buffer, MessageThreadCallback callback) { int bytes; switch (socket.GetProtocolType()) { case ProtocolType.Tcp: bytes = socket.GetSocket().Receive(Buffer.GetBufferRef(buffer)); callback(bytes); break; case ProtocolType.Udp: EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); bytes = socket.GetSocket().ReceiveFrom(Buffer.GetBufferRef(buffer), ref remoteEndPoint); callback(bytes, remoteEndPoint); break; default: callback(-1); break; } }
public static ISocket TcpAccept(ISocket listenSocket, SocketCommunicationTypes type = SocketCommunicationTypes.Blocking, Func <ISocket, Exception, Socket> callback = null) { if (type == SocketCommunicationTypes.Blocking) { return(AwesomeSocket.New(listenSocket.GetSocket().Accept())); } if (callback == null) { throw new ArgumentNullException(string.Format("{0}=null; You must provide a valid callback when using the NonBlocking type", "callback")); } new Thread(() => TcpAcceptThread(listenSocket, callback)).Start(); return(null); }
private static void TcpAcceptThread(ISocket listenSocket, Func <ISocket, Exception, Socket> callback) { Socket clientSocket = null; try { clientSocket = listenSocket.GetSocket().Accept(); } catch (Exception ex) { callback(null, ex); } callback(AwesomeSocket.New(clientSocket), null); }
/// <summary> /// This method is called each time a new client is connected. /// </summary> /// <param name="pClientSocket">The client socket.</param> /// <param name="pError">The error during connection.</param> /// <returns></returns> private Socket NewClient(ISocket pClientSocket, Exception pError) { if (pClientSocket?.GetSocket() != null) { AweSock.TcpAccept(this.ListenSocket, SocketCommunicationTypes.NonBlocking, this.NewClient); ClientView lClientView = new ClientView() { Socket = pClientSocket }; this.Clients.TryAdd(lClientView, lClientView); this.ClientConnected?.Invoke(this, lClientView, null); return(pClientSocket.GetSocket()); } return(null); }
public static bool CS_ECHO_Handler(ISocket sock, byte[] buffer) { CS_ECHO_Packet packet = PacketHelper.ParsePacketStruct<CS_ECHO_Packet>(buffer); Int16 readLen = PacketHelper.ParseBodyLen(buffer); if (readLen < PacketHelper.Size(packet)) { Console.WriteLine("read length is too small({0})", readLen); return true; // socket close } //return packet.Handle(sock, buffer); Console.WriteLine("{0}:\"{1}\" from {2}", CS_PacketType.CS_ECHO, packet.msg, sock.GetSocket().RemoteEndPoint.ToString()); // CS_ECHO_Packet 은 SC_ECHO_RP_Packet 과 호환된다... PacketHelper.Send(sock, (Int16)SC_PacketType.SC_ECHO_RP, packet); return false; }
public static int GetRemotePort(ISocket socket) { if (socket.GetSocket().ProtocolType == ProtocolType.Udp) throw new ConstraintException("Cannot get remote IP Address of a UDP socket directly. It is returned from the ReceiveMessage as the second item in the Tuple<>"); var socketEndPoint = (IPEndPoint)socket.GetRemoteEndPoint(); return socketEndPoint.Port; }
public static bool CS_LOGIN_Handler(ISocket sock, byte[] buffer) { CS_LOGIN_Packet packet = PacketHelper.ParsePacketStruct<CS_LOGIN_Packet>(buffer); Int16 readLen = PacketHelper.ParseBodyLen(buffer); if (readLen < PacketHelper.Size(packet)) { Console.WriteLine("read length is too small({0})", readLen); return true; // socket close } // do login UserLoginData iter = ChatServer.Instance().m_UserLoginDataList.Find(x => (x.name == packet.name && x.pwd == packet.pwd)); if (iter.name == packet.name) { SC_LOGIN_RESULT_Packet result = new SC_LOGIN_RESULT_Packet(); lock (ChatServer.Instance().m_ChatUserMapLock) { ChatUser tmp; if (ChatServer.Instance().m_mapChatUser.TryGetValue(packet.name, out tmp)) { result.Result = 0; } else { result.Result = 1; ChatUser newUser = new ChatUser(); newUser.name = packet.name; newUser.UserSocket = sock; sock.Owner = newUser; ChatServer.Instance().m_mapChatUser.Add(newUser.name, newUser); } } Console.WriteLine("{0} logged in.", packet.name); PacketHelper.Send(sock, (Int16)SC_PacketType.SC_LOGIN_RESULT, result); } else { SC_LOGIN_RESULT_Packet result = new SC_LOGIN_RESULT_Packet(); result.Result = 0; Console.WriteLine("{0} log in failed.({1})", packet.name, sock.GetSocket().RemoteEndPoint.ToString()); PacketHelper.Send(sock, (Int16)SC_PacketType.SC_LOGIN_RESULT, result); } return false; }
private static void TcpAcceptThread(ISocket listenSocket, Func<ISocket, Exception, Socket> callback) { Socket clientSocket = null; try { clientSocket = listenSocket.GetSocket().Accept(); } catch (Exception ex) { callback(null, ex); } callback(AwesomeSocket.New(clientSocket), null); }
public static int SendMessage(ISocket socket, string ip, int port, Buffer buffer) { if (socket.GetSocket().ProtocolType == ProtocolType.Tcp) throw new ConstraintException("Cannot call this method with a TCP socket. Call SendMessage(Socket, Buffer) instead."); return socket.SendMessage(ip, port, buffer); }
public static ISocket TcpAccept(ISocket listenSocket, SocketCommunicationTypes type = SocketCommunicationTypes.Blocking, Func<ISocket, Exception, Socket> callback = null) { if (type == SocketCommunicationTypes.Blocking) { return AwesomeSocket.New(listenSocket.GetSocket().Accept()); } if (callback == null) throw new ArgumentNullException(string.Format("{0}=null; You must provide a valid callback when using the NonBlocking type", "callback")); new Thread(() => TcpAcceptThread(listenSocket, callback)).Start(); return null; }
public static bool CS_SAY_Handler(ISocket sock, byte[] buffer) { CS_SAY_Packet packet = PacketHelper.ParsePacketStruct<CS_SAY_Packet>(buffer); Console.WriteLine("{0}:\"{1}\" from {2}", CS_PacketType.CS_SAY.ToString(), packet.msg, sock.GetSocket().RemoteEndPoint.ToString()); // 다른 유저들에게 전송 SC_SAY_Packet sendPacket = new SC_SAY_Packet(); sendPacket.sender = ((ChatUser)sock.Owner).name; sendPacket.msg = packet.msg; ChatServer.Instance().SendToAllUserWithoutMe(sock, (Int16)SC_PacketType.SC_SAY, sendPacket); // 아래는 모든 socket 에게 보내기, 위는 모든 '유저' 에게 보내기 // 유저는 로그인 성공한 녀석들만 포함. //ChatServer.Instance().m_listenServer.SendToAllWithoutMe(sock, (Int16)SC_PacketType.SC_SAY, sendPacket); return false; }