public void SendToServer(Message message) { AsyncObject ao = new AsyncObject(1); ao.Buffer = message; ao.WorkingSocket = _ClientSocket; try { _ClientSocket.BeginSend(ao.Buffer, 0, ao.Buffer.Length, SocketFlags.None, _fnSendHandler, ao); } catch (Exception ex) { if (ClientError != null) ClientError(new ClientErrorEventArgs(ex, DateTime.Now, this)); return; } }
private void send(IAsyncResult ar) { AsyncObject ao = (AsyncObject)ar.AsyncState; int sendBytes; try { sendBytes = ao.WorkingSocket.EndSend(ar); } catch (Exception ex) { if (ClientError != null) ClientError(new ClientErrorEventArgs(ex, DateTime.Now, this)); return; } if (sendBytes > 0) { byte[] msgByte = new byte[sendBytes]; Array.Copy(ao.Buffer, msgByte, sendBytes); Message msg = new Message(msgByte); if (SendMessage != null) SendMessage(new MessageEventArgs(msg, ao.WorkingSocket)); } }
private void receive(IAsyncResult ar) { AsyncObject ao = (AsyncObject)ar.AsyncState; int recvBytes; try { recvBytes = ao.WorkingSocket.EndReceive(ar); } catch (Exception ex) { if (ClientError != null) ClientError(new ClientErrorEventArgs(ex, DateTime.Now, this)); return; } if (recvBytes > 0) { byte[] msgByte = new byte[recvBytes]; Array.Copy(ao.Buffer, msgByte, recvBytes); Message msg = new Message(msgByte); if (ReceiveMessage != null) ReceiveMessage(new MessageEventArgs(msg, ao.WorkingSocket)); } try { ao.WorkingSocket.BeginReceive(ao.Buffer, 0, ao.Buffer.Length, SocketFlags.None, _fnReceiveHandler, ao); } catch (Exception ex) { if (ClientError != null) ClientError(new ClientErrorEventArgs(ex, DateTime.Now, this)); return; } }
private void connect(IAsyncResult ar) { Socket client; try { client = _ServerSocket.EndAccept(ar); } catch (Exception ex) { if (ServerError != null) ServerError(new ServerErrorEventArgs(ex, DateTime.Now, this)); return; } AsyncObject ao = new AsyncObject(4096); ao.WorkingSocket = client; User user = new User(); user.Socket = client; user.IP = ((IPEndPoint)client.RemoteEndPoint).Address.ToString(); user.Port = ((IPEndPoint)client.RemoteEndPoint).Port; user.Server = this; user.Guid = null; Users.Add(user); Message info = new Message() { Type = MessageType.Info, Text = Data.ServerName }; SendToUser(user, info); info = new Message() { Type = MessageType.Info, Text = Data.MaxUserCount.GetValueOrDefault().ToString() }; SendToUser(user, info); //info = new Message() { Type = MessageType.Info, Text = Utility.ToBase64(Data.ServerImage, ImageFormat.Png) }; //SendToUser(user, info); try { client.BeginReceive(ao.Buffer, 0, ao.Buffer.Length, SocketFlags.None, _fnReceiveHandler, ao); } catch (Exception ex) { if (ServerError != null) ServerError(new ServerErrorEventArgs(ex, DateTime.Now, this)); return; } }
public bool SendToUser(User user, Message message) { AsyncObject ao = new AsyncObject(1); ao.Buffer = message; ao.WorkingSocket = user.Socket; try { user.Socket.BeginSend(ao.Buffer, 0, ao.Buffer.Length, SocketFlags.None, _fnSendHandler, ao); } catch (Exception ex) { if (ServerError != null) ServerError(new ServerErrorEventArgs(ex, DateTime.Now, this)); return false; } return true; }
public bool SendToOther(User other, Message message) { foreach (var it in Users) { if (it == other) continue; AsyncObject ao = new AsyncObject(1); ao.Buffer = message; ao.WorkingSocket = it.Socket; try { it.Socket.BeginSend(ao.Buffer, 0, ao.Buffer.Length, SocketFlags.None, _fnSendHandler, ao); } catch (Exception ex) { if (ServerError != null) ServerError(new ServerErrorEventArgs(ex, DateTime.Now, this)); return false; } } return true; }
public MessageEventArgs(Message msg, Socket sock) { Message = msg; Sender = sock; }