本文整理汇总了C#中ISocket.GetSocket方法的典型用法代码示例。如果您正苦于以下问题:C# ISocket.GetSocket方法的具体用法?C# ISocket.GetSocket怎么用?C# ISocket.GetSocket使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ISocket
的用法示例。
在下文中一共展示了ISocket.GetSocket方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CloseSock
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);
}
示例2: OnAccepted
// 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);
}
示例3: ReceiveMessage
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
}
示例4: CS_ECHO_Handler
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;
}
示例5: CS_LOGIN_Handler
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;
}
示例6: CS_SAY_Handler
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;
}
示例7: TcpAccept
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;
}
示例8: SendMessage
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);
}
示例9: GetRemotePort
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;
}
示例10: TcpAcceptThread
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);
}
示例11: MessageReceiveThread
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;
}
}