|
@@ -124,8 +124,8 @@ namespace clientTest.chat
|
|
|
|
|
|
var port = 6000;
|
|
var port = 6000;
|
|
|
|
|
|
- //var endPoint = new IPEndPoint(IPAddress.Parse("192.168.10.17"), port);
|
|
|
|
- var endPoint = new IPEndPoint(IPAddress.Loopback, port);
|
|
|
|
|
|
+ var endPoint = new IPEndPoint(IPAddress.Parse("192.168.10.86"), port);
|
|
|
|
+ //var endPoint = new IPEndPoint(IPAddress.Loopback, port);
|
|
//var endPoint = new IPEndPoint(IPAddress.Parse("115.159.121.129"), port);
|
|
//var endPoint = new IPEndPoint(IPAddress.Parse("115.159.121.129"), port);
|
|
|
|
|
|
using (var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
|
using (var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
|
@@ -145,6 +145,7 @@ namespace clientTest.chat
|
|
{
|
|
{
|
|
try
|
|
try
|
|
{
|
|
{
|
|
|
|
+
|
|
await sendMsg(client);
|
|
await sendMsg(client);
|
|
Thread.Sleep(r.Next(1000, 1100));
|
|
Thread.Sleep(r.Next(1000, 1100));
|
|
}
|
|
}
|
|
@@ -162,7 +163,7 @@ namespace clientTest.chat
|
|
try
|
|
try
|
|
{
|
|
{
|
|
var msg = new CS_ChatLogin() { Uid = Guid.NewGuid().ToString(), Zoneid = 1, Name = names[r.Next(0, 3)] + r.Next() };
|
|
var msg = new CS_ChatLogin() { Uid = Guid.NewGuid().ToString(), Zoneid = 1, Name = names[r.Next(0, 3)] + r.Next() };
|
|
- var data = SocketDataToBytes(BytesToSocketData(eProtocalCommand.CsChatLogin, IMsg2Bytes(msg)));
|
|
|
|
|
|
+ var data = sSocketData.FromBytes(eProtocalCommand.CsChatLogin, IMsg2Bytes(msg)).ToBytes();
|
|
await Sock.SendAsync(new ArraySegment<byte>(data), SocketFlags.None);
|
|
await Sock.SendAsync(new ArraySegment<byte>(data), SocketFlags.None);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
catch (Exception e)
|
|
@@ -177,13 +178,14 @@ namespace clientTest.chat
|
|
|
|
|
|
var info = texts[r.Next(0, texts.Length)];
|
|
var info = texts[r.Next(0, texts.Length)];
|
|
var msg = new CS_ChatSendMsg() { ToChannel = c, Msg = info };
|
|
var msg = new CS_ChatSendMsg() { ToChannel = c, Msg = info };
|
|
-
|
|
|
|
- var data = SocketDataToBytes(BytesToSocketData(eProtocalCommand.CsChatSendMsg, IMsg2Bytes(msg)));
|
|
|
|
|
|
+ Console.WriteLine($"发送消息{info}");
|
|
|
|
+ var data = sSocketData.FromBytes(eProtocalCommand.CsChatSendMsg, IMsg2Bytes(msg)).ToBytes();
|
|
await sock.SendAsync(new ArraySegment<byte>(data), SocketFlags.None);
|
|
await sock.SendAsync(new ArraySegment<byte>(data), SocketFlags.None);
|
|
}
|
|
}
|
|
|
|
|
|
static byte[] IMsg2Bytes(pb::IMessage msg)
|
|
static byte[] IMsg2Bytes(pb::IMessage msg)
|
|
{
|
|
{
|
|
|
|
+
|
|
using var ms = new MemoryStream();
|
|
using var ms = new MemoryStream();
|
|
using var goutstream = new pb::CodedOutputStream(ms);
|
|
using var goutstream = new pb::CodedOutputStream(ms);
|
|
msg.WriteTo(goutstream);
|
|
msg.WriteTo(goutstream);
|
|
@@ -211,62 +213,13 @@ namespace clientTest.chat
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
|
- /// 向客户端写入消息
|
|
|
|
- /// </summary>
|
|
|
|
- static async void WriteToserver(Socket Sock)
|
|
|
|
- {
|
|
|
|
- while (true)
|
|
|
|
- {
|
|
|
|
- var msg = await sendDataBuffer.Reader.ReadAsync();
|
|
|
|
- var data = SocketDataToBytes(msg);
|
|
|
|
- await Sock.SendAsync(new ArraySegment<byte>(data), SocketFlags.None);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- /// <summary>
|
|
|
|
- /// 网络结构转数据
|
|
|
|
- /// </summary>
|
|
|
|
- /// <param name="tmpSocketData"></param>
|
|
|
|
- /// <returns></returns>
|
|
|
|
- static private byte[] SocketDataToBytes(sSocketData tmpSocketData)
|
|
|
|
- {
|
|
|
|
- byte[] _tmpBuff = new byte[tmpSocketData.PackLen];
|
|
|
|
- byte[] _tmpBuffLength = BitConverter.GetBytes(tmpSocketData.PackLen);
|
|
|
|
- byte[] _tmpDataLenght = BitConverter.GetBytes((UInt16)tmpSocketData._protocallType);
|
|
|
|
-
|
|
|
|
- Array.Copy(_tmpBuffLength, 0, _tmpBuff, 0, Constants.HEAD_DATA_LEN);//缓存总长度
|
|
|
|
- Array.Copy(_tmpDataLenght, 0, _tmpBuff, Constants.HEAD_DATA_LEN, Constants.HEAD_TYPE_LEN);//协议类型
|
|
|
|
- Array.Copy(tmpSocketData._data, 0, _tmpBuff, Constants.HEAD_LEN, tmpSocketData.DataLen());//协议数据
|
|
|
|
-
|
|
|
|
- return _tmpBuff;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- /// <summary>
|
|
|
|
- /// 数据转网络结构
|
|
|
|
- /// </summary>
|
|
|
|
- /// <param name="_protocalType"></param>
|
|
|
|
- /// <param name="_data"></param>
|
|
|
|
- /// <returns></returns>
|
|
|
|
- static private sSocketData BytesToSocketData(eProtocalCommand _protocalType, byte[] _data)
|
|
|
|
- {
|
|
|
|
- sSocketData tmpSocketData = new sSocketData();
|
|
|
|
- tmpSocketData.PackLen = Constants.HEAD_LEN + _data.Length;
|
|
|
|
- // tmpSocketData._dataLength = _data.Length;
|
|
|
|
- tmpSocketData._protocallType = _protocalType;
|
|
|
|
- tmpSocketData._data = _data;
|
|
|
|
- return tmpSocketData;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 接收buffer
|
|
/// 接收buffer
|
|
/// </summary>
|
|
/// </summary>
|
|
static private Channel<sSocketData> recvDataBuffer = Channel.CreateUnbounded<sSocketData>();
|
|
static private Channel<sSocketData> recvDataBuffer = Channel.CreateUnbounded<sSocketData>();
|
|
- /// <summary>
|
|
|
|
- /// 发送buffer
|
|
|
|
- /// </summary>
|
|
|
|
- static private Channel<sSocketData> sendDataBuffer = Channel.CreateUnbounded<sSocketData>();
|
|
|
|
|
|
+
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 接收客户端发来的信息,客户端套接字对象
|
|
/// 接收客户端发来的信息,客户端套接字对象
|